Join us on Discord!
You can help CodeWalrus stay online by donating here.

question about C

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

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

James Oldiges

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).
  • Calculators owned: TI-84+
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.
  • Calculators owned: TI-83+.fr, TI-Nspire CAS prototype, TI-84+ CSE, TI-Nspire CX
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...
  • Calculators owned: TI-84+
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).
  • Calculators owned: A lot.

James Oldiges

#5
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.
  • Calculators owned: TI-84+
Legends say if you spam more than DJ Omnimaga, you will become a walrus...


Dream of 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?
  • Calculators owned: TI-82 Advanced Edition Python TI-84+ TI-84+CSE TI-84+CE TI-84+CEP TI-86 TI-89T cfx-9940GT fx-7400G+ fx 1.0+ fx-9750G+ fx-9860G fx-CG10 HP 49g+ HP 39g+ HP 39gs (bricked) HP 39gII HP Prime G1 HP Prime G2 Sharp EL-9600C
  • Consoles, mobile devices and vintage computers owned: Huawei P30 Lite, Moto G 5G, Nintendo 64 (broken), Playstation, Wii U

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