【问题标题】:Variable warning set but not used变量警告集但未使用
【发布时间】:2014-06-02 15:37:36
【问题描述】:
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 部分。

【问题讨论】:

  • 您是否在某处使用了该值?
  • 警告正是它所说的,你设置了none,但从不使用它的值。你所做的就像说int myNum = 0; 然后在其他任何地方都不使用myNum。
  • 但是我用过。 "无[i] = number1;ntwo[i] = number2;"
  • 那里看不到,但它处于循环中,将运行 5 次。 i 值将从 1 变为 5。

标签: c variables warnings


【解决方案1】:

none 在这段代码 sn-p 中出现了两次:

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 是“已使用”,但正如代码所示,你有:"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 正在被使用,您的打印正在做一些有用的事情...

【讨论】:

  • 当编译器说“使用”时,它的意思是“使用值”......因此应该从某处读取 none[] 元素以消除警告。
  • @MikeW - 我不确定我是否理解您的评论。是的,警告在那里,因为 OP 没有使用 none 数组。 OP 不小心将它添加到了 print 语句中,因此发出了警告。我认为这很清楚。您是否尝试添加它?
  • OP 似乎误解了“使用”一词,这是它的日常意义,即。 “我提到了变量”,而不是似乎适用于编译器警告的意思,“变量的值用于形成另一个结果”
【解决方案2】:

使用变量不同于初始化它。

您在这里为 none 变量设置了一个值,但您的编译器会告诉您它未使用,因为您从未使用比较运算符对其进行测试,或者您从未将其传递给函数。

【讨论】:

    【解决方案3】:

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

    在第一个错误上失败通常有助于提高工作效率和专注度。但有时它会咬你。这是我自己的代码有错误,我花了一段时间才找到。

    为了让你更容易找到我发表评论: “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 -------- **/ 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多