Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable warning set but not used

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)

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.

like image 295
user3661531 Avatar asked Jun 02 '14 15:06

user3661531


2 Answers

none shows up twice in this code snippet:

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 none is "used", but as the code is, you have: "none" set but not used; exactly what the compiler said.


In the pastebin link I see your problem:

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 none is being used and your print is doing something useful...

like image 183
Mike Avatar answered Sep 24 '22 17:09

Mike


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.

like image 44
Antoine C. Avatar answered Sep 25 '22 17:09

Antoine C.