CodeWalrus

Development => PC, Mac & Vintage Computers => Topic started by: James Oldiges on January 14, 2015, 02:11:23 PM

Title: question about C
Post by: James Oldiges on January 14, 2015, 02:11:23 PM
Int a [4]
Int b[4]
a[6] = 5


OK, my professor heavily implied that will always result/be the same thing as b[1] =5.  Can anyone confirm this, as I'm 99% sure that it's not true...

Thanks!
Title: Re: question about C
Post by: Snektron on January 14, 2015, 02:36:48 PM
I'm relatively new to C, but it's easy to test:

int a[4];
int b[4];
a[6] = 5;
printf("%d",b[1]);

on my PC this results in "4201520" .
I suspect that this a compiler thing is though...
I'm using mingw, but if you have a compiler which
parses  int a[4]  as "make a 4*4 bytes large datablock here",
it could work (I'm not really sure if some compilers work this way).
Title: Re: question about C
Post by: matrefeytontias on January 14, 2015, 03:49:00 PM
Even if it's true, don't do that. That's horrible.
Title: Re: question about C
Post by: Snektron on January 14, 2015, 04:10:26 PM
Yeah... He's probably not much better than my programming teacher...
Title: Re: question about C
Post by: Duke "Tape" Eiyeron on January 14, 2015, 05:56:56 PM
Quote from: Cumred_Snektron on January 14, 2015, 02:36:48 PM
I'm relatively new to C, but it's easy to test:

int a[4];
int b[4];
a[6] = 5;
printf("%d",b[1]);

on my PC this results in "4201520" .
I suspect that this a compiler thing is though...
I'm using mingw, but if you have a compiler which
parses  int a[4]  as "make a 4*4 bytes large datablock here",
it could work (I'm not really sure if some compilers work this way).


Actually, you can't really know what's happening. SOmetimes the compiler directly removes the variable as it's not used or their memory zones aren't adjacent all the time. Don't ever do that as Matref said, it's like a russian roulette (hello to the snake).
Title: Re: question about C
Post by: James Oldiges on January 14, 2015, 06:55:36 PM
Alright, thanks.
I didn't think it would work, but I wasn't certain enough to correct the professor.


The class is...  interesting. He will have code exmples on the projector, but never has them run nor compiled.
Title: Re: question about C
Post by: Snektron on January 14, 2015, 08:19:21 PM
Mine's favourite language is python and thinks it can actually run faster than the
language it's written in. We ran a simple test vs java and java - even JAVA - can
beat it hands down (although this may be the interperter's fault). Anyway, he hates me
and my friend b/c we hate Python and inspects our projects thoroughly for any
mistakes while others get grades by just looking at their work.
Title: Re: question about C
Post by: Dream of Omnimaga on January 14, 2015, 11:30:16 PM
Quote from: James Oldiges on January 14, 2015, 06:55:36 PM
The class is...  interesting. He will have code exmples on the projector, but never has them run nor compiled.
I hate when I have a computer class and the teacher is less experienced than most of the classroom. >.< If  you ever need help, then you are in a bit of trouble. Is he really giving you code examples that don't always work?
Title: Re: question about C
Post by: JamesV on January 15, 2015, 12:05:06 AM
Like Eiyeron said, it might work, but that would just be pure luck. The variables will be assigned to wherever there is free memory - whether or not that happens to place them adjacent to each other (and with "a" preceding "b") is (essentially) random.

Not a good habit to get into :)