C语言 变量警告设置但未使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23998283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:07:48  来源:igfitidea点击:

Variable warning set but not used

cvariableswarnings

提问by user3661531

int none[5];
int ntwo[5];

(the following is in a switch statement);

    if (answer == userAnswer)
{
    printf("Correct!\n");
    score = prevScore + 1;
    prevScore = score;
}

else
{
    printf("Incorrect. The correct answer was %d\n\n", answer); 
    none[i] = number1;
    ntwo[i] = number2;
}
}
break;

(Switch statement ends)

(switch语句结束)

It gives me an error saying "Variable warning "none" set but not used". I have clearly used it. I dont know why this error i happening. FYI all the other variables you see have been declared. I just took out the imp part where the array appears.

它给了我一个错误,说“变量警告“无”设置但未使用”。我已经清楚地使用了它。我不知道为什么我会发生这个错误。仅供参考,您看到的所有其他变量都已声明。我只是取出了数组出现的imp部分。

回答by Mike

noneshows up twice in this code snippet:

none在此代码片段中出现两次:

int none[5]; // declared, not set to anything

And then:

进而:

none[i] = number1; // a value has been set, but it's not being used for anything

If, for example, you later had:

例如,如果您后来有:

int foo = none[3];  // <-- the value in none[3] is being used to set foo

or

或者

for(int i = 0; i < 5; i++)
    printf("%d\n", none[i]);   // <-- the values in none are being used by printf

or something to that effect, we would say noneis "used", but as the code is, you have: "none" set but not used; exactly what the compiler said.

或者诸如此类的话,我们会说none是“拿来主义”,但作为代码,您有:"none" set but not used; 正是编译器所说的。



In the pastebin linkI see your problem:

pastebin 链接中,我看到了您的问题:

You wrote this:

你写了这个:

for(i=0;i<5;i++)
{
    printf("Question [i]: none[i]+ntwo[i]");

You meant to write this:

你打算写这个:

for(i=0;i<5;i++)
{
    printf("Question [i]: ", none[i]+ntwo[i]);

Now noneis being used and your print is doing something useful...

现在none正在使用并且您的打印正在做一些有用的事情......

回答by Antoine C.

Using a variable is different from initializing it.

使用变量与初始化变量不同。

Here you set a value to the none variable, but your compiler will tell you it's unused because you never test it with comparison operators, or you never pass it to a function.

在这里,您为 none 变量设置了一个值,但是您的编译器会告诉您它未使用,因为您从未使用比较运算符对其进行测试,或者您从未将其传递给函数。