开发者

warning: unused variable

see in one code i have written

void my_function()
{
INT32开发者_StackOverflow中文版 i;    /* Variable for iteration */ 
/* If system is little-endian, store bytes in array as reverse order */
#ifdef LITTLE   
   {    
    // i m using i for operating one loop 
   }
#endif

 /* If the system is big-endian, store bytes in array as forward order */
#ifdef  BIG 
   {
   // using i for loop
   }
#endif
return;
 }

by compiling this code with -Wall flag it shows

 warning: unused variable ‘i’      

why? how can i remove this?


Put the declaration of i just inside the {} where you actually use it. Even better if you have C99, declare the loop variable inside the for(int i = 0, i < bound; ++i)


By defining either LITTLE or BIG. I doubt many compilers give that warning for this code when one of those symbols are defined. If you still have the warning, then you might change the second #ifdef to an #else.

I don't see any #endif anywhere -- presumably in the real code, those appear.


You need to define either BIG or LITTLE (as mentioned elsewhere).

In order to stop this happening again in the future, you can raise a specific compile-time error using the following:

#if !defined LITTLE && !defined BIG
#error You haven't defined one of your macro names
#endif

Alternatively, you could include i only when using either code block by surrounding it with #if defined as well:

#if defined LITTLE || defined BIG
INT32 i;
#endif

In both cases, note the keyword to use is #if not #ifdef or #ifndef.


You do not have to declare the iteration number. Just do it in your for statement.

for(int i = 0; i < 6; i++){
  // insert loop code here

}


hey i got the answer...

see BIG & LITTLE are preposser flag & they are given at compile time. when i was compiling my project with make file i was giving this flag at compile time but while testing each individual file i was compiling like

gcc -Wall -c filename.c 

This was comming because i havent given any flag so compiler going to neglet that much portion of code & i was getting warning.

gcc -Wall -c -LITTLE filename.c 

works perfectly...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜