Variable warning set but not used

76,632

Solution 1

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...

Solution 2

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.

Share:
76,632
user3661531
Author by

user3661531

Updated on August 07, 2020

Comments

  • user3661531
    user3661531 over 3 years
    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.

  • MikeW
    MikeW almost 8 years
    When the compiler says "used", it means "value used" ... hence an element of none[] should be read from, somewhere, to defeat the warning.
  • Mike
    Mike almost 8 years
    @MikeW - I'm not sure I understand your comment. Yes, the warning is there because the OP didn't use the none array. The OP accidentally added it into the print statement hence the warning. I think that much was clear. Were you trying to add to that?
  • MikeW
    MikeW almost 8 years
    The OP seemed to misunderstand the term "used", to be its everyday sense, ie. "I mentioned the variable" as opposed to the sense which appears to apply in the compiler warning, "value of variable used to form another result"