加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

错误:在文件范围内修改了’d’

发布时间:2020-12-16 09:50:24 所属栏目:百科 来源:网络整理
导读:代码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]; 此代码显示错误:
代码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 2中,而Code 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读