user3661531
user3661531

Reputation: 175

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.

Upvotes: 16

Views: 109946

Answers (4)

skittlebiz
skittlebiz

Reputation: 377

Your last use of it is in code:

    none[i] = number1;

then nothing is done with it. What is the purpose of this variable afterward? You may as well completely remove it everywhere. Try removing it completely, as it's useless - that last line of code is pointless, because none[i] is never used again. I have a feeling you are leaving out some code that makes it obvious that this is the case, because your comment says you left out an imp part, whatever imp means.

Upvotes: 0

KANJICODER
KANJICODER

Reputation: 3885

If other answers didn't work: CHECK YOUR BRACKET BALANCING.

Failing on the first error is usually nice for productivity and focus. But sometimes it can come to bite you. Here is my own code that has the error and it took me a while to find.

To make it easier for you to find I put the comment: "ERROR_IS_ON_LINE_BELOW_EXTRA_CURLY_BRACKET" on the line directly above the problematic line.

This code is not the minimal example, but rather the entire file I was working on at the time I encountered the problem. I thought it would be more beneficial to show what the problem actually looked like for me than to contrive something.

/** CAV: C_Any_Version                                       **/
/**      Should be simple enough to work in all versions     **/
/**      of C.                                               **/

/** [+]: REFACTOR_TO: (+)  --------------------------------- **/
/** [!]: REFACTOR_TO: (!)  --------------------------------- **/
/** [-]: REFACTOR_TO: (!)  --------------------------------- **/

/** (+): Ok Message                ------------------------- **/
/** (!): Error Message             ------------------------- **/
/** (-): Refactor these to be  [!] ------------------------- **/

#define JTC_MAC_BUF_MAX ( 1024 * 1024 )
#define JTC_MAC_NIL     ((void*)0)

char buf_inn[ JTC_MAC_BUF_MAX ]; /** TEX_INN  **/
char buf_out[ JTC_MAC_BUF_MAX ]; /** TEX_OUT  **/

int nob_inn =( 0-888 ); /** Number_Of_Bytes: buf_inn **/
int nob_out =( 0-888 ); /** Number_Of_Bytes: buf_out **/

#include <stdio.h> /** for: fopen(...) **/
#include <stdio.h>  


void JTC_Say( const char* jtc_say ){
    printf( "[JTC_Say]:%s\n", jtc_say );
}
void JTC_Say_001( const char* jtc_say ){
    printf( "[JTC_Say_001]:%s\n", jtc_say );
}





signed char JTC_Put_buf_inn( /** void **/ ){

/** -------------------------------------------------------- **/

    /** VARIABLE_DECLARATIONS **/

    signed      char       ok; /** 0 if everything is ok.    **/
    FILE*             tex_poi; /** Pointer To Text File      **/
    long              tex_nob; /** Number_Of_Bytes           **/
                int   seek_ok; /** Seek success code : 0     **/
    long        int   offset0; /** Offset by 0 when seek.    **/
                int   nob_got; /** Number of read bytes.     **/
                int   sin_oct; /** Size in octetets(bytes)   **/
 
/** -------------------------------------------------------- **/

    /** VARIABLE_INIT **/

         ok=( 1  ); /** No problems at the moment.           **/
    seek_ok=( 0  ); /** Seek returns true on success         **/
    offset0=( 0L ); /** offset by nothing                    **/
    sin_oct=( 1  ); /** Size of each element is 1 byte.      **/

/** ERROR_IS_ON_LINE_BELOW_EXTRA_CURLY_BRACKET **/
};;if( ok ){ /** ........................................... **/

    /** OPEN_TEXT_FILE **/

    tex_poi = fopen( "./EXPENDABLE_TEST_FILE._" , "r" );
    if( tex_poi == JTC_MAC_NIL ){ 
        (ok=0);(JTC_Say_001("[UNABLE_TO_OPEN_FILE]"));
    };;
 
};;if( ok ){ /** ........................................... **/

    /** SEEK_TO_THE_END_OF_THE_FILE **/
    
    if( seek_ok != fseek( tex_poi, offset0, SEEK_END ) ){
        (ok=0);(JTC_Say_001("[FAILED_TO_SEEK_TO_END]"));
    };;
  
};;if( ok ){ /** ........................................... **/

    /** GET_SIZE_OF_FILE **/

    tex_nob = ftell( tex_poi );
    if( tex_nob < 0 ){ 

        (ok=0);(JTC_Say_001("[UNABLE_TO_GET_FILE_SIZE]"));
    
    };;
 
};;if( ok ){ /** ........................................... **/

    /** MEMORY_CHECK **/

    /** Check to see if we have enough memory to copy file   **/
    /** (tex_nob+1) because we need to make room for the     **/
    /** null terminator at the end of the string.            **/
    if( (tex_nob+1) > JTC_MAC_BUF_MAX ){
        
        (ok=0);(JTC_Say_001("[NOT_ENOUGH_MEMORY]"));

    };;
    
};;if( ok ){ /** ........................................... **/

    /** SEEK_BACK_TO_START_OF_FILE **/
    /** Cannot read file contents unless we first rewind **/

    if( seek_ok != fseek( tex_poi, offset0, SEEK_SET ) ){
        (ok=0);(JTC_Say_001("[FAILED_TO_REWIND_FILE_POINTER]"));
    };;

};;if( ok ){ /** ........................................... **/

    /** READ_FILE_INTO_MEMORY **/

    nob_inn=( tex_nob );  
    nob_got = fread( buf_inn, sin_oct , nob_inn , tex_poi );

    printf("[nob_inn]:%d\n", nob_inn );
    printf("[buf_inn]:%s\n", buf_inn );
    printf("[nob_got]:%d\n", nob_got );

    if( nob_got != nob_inn ){
        (ok=0);(JTC_Say_001("[FREAD_DIFFERENT_AMOUNT]"));
    };;

};;
/** -------------------------------------------------------- **/
    return( 0x01-ok );
/** -------------------------------------------------------- **/
}





/** Save processed[ buf_out ]to text file. **/
void JTC_Put_buf_out( /** void **/ ){

    /** TODO **/

}

void JTC_Par( void ){

    /** TODO **/

}

int main( void ){
    printf("[BEG:main]\n");

    JTC_Put_buf_inn(); /** INN: JS code **/

    JTC_Par(); /** Parsing logic here **/

    JTC_Put_buf_out(); /** OUT: C code **/
    
    printf("[END:main]\n");
    return( 0 );
}  


/** Be nice to other libraries and clean up after yourself **/
#undef JTC_MAC_NIL
#undef JTC_MAC_BUF_MAX



/** [JTC_Put_buf_inn] : LOAD_TEXT_FILE_INTO_buf_inn -------- **/ 

Upvotes: 0

Antoine C.
Antoine C.

Reputation: 3952

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.

Upvotes: 3

Mike
Mike

Reputation: 49403

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

Upvotes: 19

Related Questions