变量警告设置但未使用

use*_*531 9 c variables warnings

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;
Run Code Online (Sandbox Code Playgroud)

(Switch语句结束)

它给我一个错误,说"变量警告"没有"设置但未使用".我已经清楚地使用过了.我不知道为什么这个错误我发生了.仅供参考,您看到的所有其他变量都已声明.我刚拿出阵列出现的imp部分.

Mik*_*ike 15

none 在此代码段中显示两次:

int none[5]; // declared, not set to anything
Run Code Online (Sandbox Code Playgroud)

然后:

none[i] = number1; // a value has been set, but it's not being used for anything
Run Code Online (Sandbox Code Playgroud)

例如,如果您以后有:

int foo = none[3];  // <-- the value in none[3] is being used to set foo
Run Code Online (Sandbox Code Playgroud)

要么

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

或类似的东西,我们会说none是"使用",但正如代码所示,你有:"none" set but not used; 正是编译器所说的.


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

你写了这个:

for(i=0;i<5;i++)
{
    printf("Question [i]: none[i]+ntwo[i]");
Run Code Online (Sandbox Code Playgroud)

你打算写这个:

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

现在none正在使用,你的印刷品正在做一些有用的事情......

  • OP似乎误解了“二手”一词,即其日常含义,即。“我提到了变量”,而不是编译器警告中应用的含义,“变量的值用于形成另一个结果” (2认同)