【问题标题】:variable modified ''at file scope变量修改''在文件范围内
【发布时间】:2020-12-29 13:49:36
【问题描述】:

我有一个程序出现错误:

variable modified 'artikel'at file scope
char artikel[ARTNAME];

我想我必须添加一个#define,但我不明白具体是哪一个。

这是我的代码:

#include <stdio.h>
#include <string.h>

const int ARTNAME = 100;

typedef struct artikel {
    char artikel[ARTNAME];
    int anzahl;
} artikel;

int main()
{
}

【问题讨论】:

  • 我添加了#define ARTNAME 100,现在它给了我----> main.c:5:20: error: expected identifier or '(' before numeric constant #define ARTNAME 100跨度>
  • 无法复制:godbolt.org/z/1Mo69q
  • 要按原样编译此代码,您可以打开C99 或更高标准。它不会通过旧的C 标准。
  • @flavio,你没有添加 #define。您将ARTNAME 的现有声明更改#define

标签: c


【解决方案1】:

我有一个程序出现错误:variable modified 'artikel'at file scope char artikel[ARTNAME];

我认为您没有准确地复制诊断信息。它肯定是在抱怨 struct artikel可变修改。

这与Variably modified array at file scope in C 中涉及的问题密切相关。具体来说,尽管有const 限定,ARTNAME 不是 C 中的“常量表达式”,因此数组artikel.artikel 的类型以及整个struct artikel 的类型都是“可变修改的”。不能在文件范围内声明具有可变修改类型的对象。

我想我必须添加一个#define,但我不明白到底是哪一个。

最快的解决方案可能是改变这个......

    const int ARTNAME = 100;

...到:

#define ARTNAME 100

【讨论】:

    猜你喜欢
    • 2017-08-05
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2015-08-08
    • 2014-03-07
    相关资源
    最近更新 更多