变量警告设置但未使用

时间:2014-06-02 15:37:37

标签: 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;

(Switch语句结束)

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

3 个答案:

答案 0 :(得分:14)

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

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

然后:

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

例如,如果你以后有:

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

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

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


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

你写了这个:

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

你打算写这个:

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

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

答案 1 :(得分:3)

使用变量与初始化不同。

在这里你设置一个值为none变量,但你的编译器会告诉你它没有被使用,因为你从来没有用比较运算符测试它,或者你从未将它传递给函数。

答案 2 :(得分:0)

如果其他答案无效:检查支架平衡

如果没有第一个错误,通常可以提高工作效率和专注力。但是有时候它会咬你。这是我自己的错误代码,花了我一段时间才找到。

为了使您更容易找到,我发表了评论: 在线上的“ ERROR_IS_ON_LINE_BELOW_EXTRA_CURLY_BRACKET” 直接在问题线上方。

此代码不是最小的示例,而是整个示例 我遇到问题时正在处理的文件。 我认为表明问题所在会更有益 实际上对我来说比想做点什么更重要。

/** 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 -------- **/