Alternatively, join us on Discord.

question about C

Started by James Oldiges, January 14, 2015, 02:11:23 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

James Oldiges

January 14, 2015, 02:11:23 pm Last Edit: January 14, 2015, 02:13:45 pm by aeTIos
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!

Snektron

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).
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


matrefeytontias

Even if it's true, don't do that. That's horrible.
My TI games (some got their own article on non-calc websites !) : http://www.ticalc.org/archives/files/authors/112/11202.html

My moozik (100% free metal) : http://www.soundcloud.com/matrefeytontias

Snektron

Yeah... He's probably not much better than my programming teacher...
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Duke "Tape" Eiyeron

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).

James Oldiges

January 14, 2015, 06:55:36 pm #5 Last Edit: January 14, 2015, 06:58:48 pm by James Oldiges
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.

Snektron

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.
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


DJ Omnimaga

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?

JamesV

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 :)

Powered by EzPortal