Omnimaga

Calculator Community => TI Calculators => Calculator C => Topic started by: lkj on September 07, 2011, 02:28:05 pm

Title: Problem with C code
Post by: lkj on September 07, 2011, 02:28:05 pm
I have a problem with the following  code:

Code: [Select]
for(w = 0, w < levelheader->number_of_eggs, ++w){
        //do something
}
Where levelheader is a pointer to a struct:
Code: [Select]
typedef struct LEVElHEADER{
//...
    uint16_t number_of_eggs;
//...
}levelheader_t;
When I try to compile it, I get these errors :
test.c:7:18: warning: value computed is not used
test.c:7:54: error: expected ';'  before ')' token

I just can't find the problem ???
Title: Re: Problem with C code
Post by: calc84maniac on September 07, 2011, 02:59:50 pm
In a for loop, you need to separate the statements with semicolons, not commas.
Title: Re: Problem with C code
Post by: lkj on September 07, 2011, 03:06:20 pm
Thank you
Sometimes I make really silly mistakes...
Title: Re: Problem with C code
Post by: fb39ca4 on September 07, 2011, 04:42:16 pm
You're not alone, I kept on doing that for a while too :P
Title: Re: Problem with C code
Post by: Munchor on September 07, 2011, 04:55:00 pm
By the way, you used "++w", shouldn't it be "w++"? I've seen both being used but I can't understand the different.

EDIT: Oh wait, I got it (http://www.gamedev.net/topic/337133-i-vs-i/ (http://www.gamedev.net/topic/337133-i-vs-i/))
Title: Re: Problem with C code
Post by: Ashbad on September 07, 2011, 05:01:59 pm
By the way, you used "++w", shouldn't it be "w++"? I've seen both being used but I can't understand the different.

very different.  they both increment, but ++w applies before the value is taken into account, w++ applies after the whole line has been run through.
Title: Re: Problem with C code
Post by: Tribal on September 08, 2011, 12:32:30 am
By the way, you used "++w", shouldn't it be "w++"? I've seen both being used but I can't understand the different.

EDIT: Oh wait, I got it (http://www.gamedev.net/topic/337133-i-vs-i/ (http://www.gamedev.net/topic/337133-i-vs-i/))

Just as a side note, it is more efficient to do ++w.  So if you don't need the value of w before it's incremented, then use ++w instead.
Title: Re: Problem with C code
Post by: calcdude84se on September 08, 2011, 12:47:25 am
Just as a side note, it is more efficient to do ++w.  So if you don't need the value of w before it's incremented, then use ++w instead.
Except any good compiler will compile "w++" to the same as "++w" if the result isn't used. (I know the discussion ephan linked says that this might not be the case sometimes, but that's for C++) If precedent helps, I've always seen it written postfix for the loop increment.