c - 错误 : variably modified 'd' at file scope

标签 c arrays struct compiler-errors

代码 1:-

int size;

struct demo
{
    int a;
};

int main()
{
    scanf("%d",&size);
    struct demo d[size];
    return 0;
}

这段代码工作正常。

代码 2:-

int size;

struct demo
{
    int a;
};

int main()
{
    scanf("%d",&size);
    return 0;
}

struct demo d[size];

此代码显示错误:-

error : variably modified 'd' at file scope

为什么这样的错误出现在 Code 2Code 1 运行正常?

最佳答案

在代码 2 中,您的结构数组驻留在定义中的数据段中

A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. The size of this segment is determined by the values placed there by the programmer before the program was compiled or assembled, and does not change at run-time.

关于c - 错误 : variably modified 'd' at file scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21672639/

相关文章:

enums - 在 powershell 中使用 add-type 处理 intPtr 和编码

c - 结构和段错误

c - 我将如何在 linux c 编程中使用 setsockopt 和 getsockopt 与 KEEP_ALIVE 来确定断开的 tcp/ip 连接?

c - 限制 C 中寄存器的使用

java - 使用 Java 8 收集数组/列表中的文件名

javascript - 为什么数组迭代将我的自定义原型(prototype)函数显示为项目?

ruby:对两个或多个数组的相应成员求和

c++ - 结构中的默认值(字符)

c - Unix socket编程如何获取本地IP地址和端口?

c - 为什么我们要在数字后面加上后缀?