Author Topic: Problem with C code  (Read 3721 times)

0 Members and 1 Guest are viewing this topic.

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Problem with C code
« 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 ???

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Problem with C code
« Reply #1 on: September 07, 2011, 02:59:50 pm »
In a for loop, you need to separate the statements with semicolons, not commas.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Problem with C code
« Reply #2 on: September 07, 2011, 03:06:20 pm »
Thank you
Sometimes I make really silly mistakes...

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: Problem with C code
« Reply #3 on: September 07, 2011, 04:42:16 pm »
You're not alone, I kept on doing that for a while too :P

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Problem with C code
« Reply #4 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/)
« Last Edit: September 07, 2011, 04:55:29 pm by ephan »

Ashbad

  • Guest
Re: Problem with C code
« Reply #5 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.

Offline Tribal

  • The Fallen
  • LV5 Advanced (Next: 300)
  • *
  • Posts: 218
  • Rating: +15/-1
    • View Profile
Re: Problem with C code
« Reply #6 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/)

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.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: Problem with C code
« Reply #7 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.
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.