Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gcc gives warning: operation on 'variable' man be undefined 1

Status
Not open for further replies.

flugh

Technical User
Aug 23, 2002
655
US
Greetings. First post to this forum.

Trying to compile this code on my Linux box using gcc 3.3.3. I get the following warning:
[tt]
gcc -Wall -O -ggdb -Dunix -DNOCRYPT bitsys.c -c -o obj/bitsys.o
bitsys.c: In function `str_flag_string':
bitsys.c:80: warning: operation on `toggle' may be undefined
[/tt]
The code in question is:
[tt]
75 char *str_flag_string(const struct flag_type *flag_table, FLAG * bits)
78 static int toggle;
79
80 toggle = (++toggle) % 10;
[/tt]
I'm really not sure how to fix the warning. Initializing it in the function will munge the static property right? The code compiles, but no warnings is a requirement. Any advice would be appreciated (slow replies though, I work a lot the first part of the week). By the way, it's some MUD code in case the context matters to anyone. I don't think that's important with this warning though.

----
JBR
 
You have ++ operation with side effect in your expression. Remember: x = y is an expression in C. Avoid such constructs because of C language does not define the moment of toggle value ++ modification. Split this expression statement if you want to write C Standard conformed code:
Code:
++toggle;
toggle %= 10;
 
Since toggle is a static, it will need an initial value.
Code:
char *str_flag_string...
{
   static int toggle = 0;
   toggle = (toggle + 1) % 10;
 
All statics have initial value 0 by default in C/C++ (yes, it's a good style to initialize explicitly;)...
 
xwb: thanks. I avoided that explicit initialzation because it looks like there's some recursion involved and I didn't want to muck up that part up :)

ArkM: I split up the line like you suggested and it compiles without warning. I appreciate it!

I'm just working with C again after a couple years away. Trying to get a feel for someone else's code can be a booger, especially when you are old and rusty like me ;-)

----
JBR
 
Statics are special - they do not re-initialize on re-entry. They are only initialized once at the start of the program.
 
... they're basically a global variable that the compiler pretends is local.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top