d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Some Questions About A Small Piece Of C Code
Prev12
Add Reply New Topic New Poll
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Aug 20 2013 02:13pm
nvm

This post was edited by Richter on Aug 20 2013 02:13pm
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Aug 20 2013 02:35pm
Quote (flyinggoat @ Aug 20 2013 11:14am)
have you tested this? cause it has 2 mistakes



Sure have and what mistakes are those?

This post was edited by SelfTaught on Aug 20 2013 02:37pm
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Aug 20 2013 02:43pm
sizeof(a) on arrays is ambiguous and will only report the size of the pointer with some compilers.
++i in the for loop increments i before it starts the first iteration. your loop starts at 1 instead of 0
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Aug 20 2013 03:29pm
Quote (flyinggoat @ 20 Aug 2013 21:43)
++i in the for loop increments i before it starts the first iteration. your loop starts at 1 instead of 0

look at this:
Code
#include <stdio.h>
void main(void){
 for(int i=0; i<5; ++i)
   printf("i: %d\n", i);
}

result is this:
Code
i: 0
i: 1
i: 2
i: 3
i: 4

because:
the last part in the for loop is executed AFTER the stuff in the for loop. therefore it doesn't matter if its i++ or ++i (in this case)

This post was edited by Richter on Aug 20 2013 03:31pm
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Aug 20 2013 06:03pm
Quote (Richter @ Aug 20 2013 11:29pm)
the last part in the for loop is executed AFTER the stuff in the for loop. therefore it doesn't matter if its i++ or ++i (in this case)


indeed, my bad.

Member
Posts: 580
Joined: Aug 8 2012
Gold: Locked
Trader: Scammer
Warn: 20%
Aug 28 2013 02:09pm
ye
Go Back To Programming & Development Topic List
Prev12
Add Reply New Topic New Poll