简体   繁体   English

错误:初始化元素不是常数 C

[英]error: initializer element is not constant C

I keep geting我不断得到

`error: initializer element is not constant
 FILE *file = fopen("/sys/class/hwmon/hwmon0/temp1_input", "r");

temperatura.c:19:14: error: expected ‘)’ before string constant
 fscanf(file ,"%s", &temp);`;

my entire code looks like:我的整个代码看起来像:

#define  temp4M   1000
FILE *file = fopen("/sys/class/hwmon/hwmon0/temp1_input", "r");
char temp[temp4M];


fscanf(file ,"%s", &temp);

int main()
{
    printf(" CPU cores :%d\n", corCPU);
    printf("%s", modCPU);
    printf("%d",temp);

    return 0;
}

is the problem in my file path or am I missing something, note that I am reading numbers from temp1_input as string instead of numbers.是我的文件路径有问题还是我遗漏了什么,请注意,我正在从 temp1_input 读取数字作为字符串而不是数字。 what is wrong?怎么了?

Code should go inside the main function:代码应该在main函数中:

int main()
{
    FILE *file = fopen("/sys/class/hwmon/hwmon0/temp1_input", "r");
    char temp[temp4M];

    fscanf(file ,"%s", &temp);

    ...
}

The compiler got confused by your code, which was outside the function, and mistook it for something.编译器被你的代码弄糊涂了,它在函数之外,把它误认为是什么。 So it gave you confusing error messages, which don't help you see the actual problem.因此,它给了您令人困惑的错误消息,这无助于您了解实际问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM