You can help CodeWalrus stay online by donating here. | New CodeWalrus | Old (dark mode) | Old (light) | Discord server

question about C

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

0
b/PC, Mac & Vintage Computers publicado por u/James Oldiges 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!
Last Edit: January 14, 2015, 02:13:45 PM by aeTIos
Inicia sesión o crea una cuenta para dejar un comentario
u/Snektron 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).
u/matrefeytontias January 14, 2015, 03:49:00 PM
Even if it's true, don't do that. That's horrible.
u/Snektron January 14, 2015, 04:10:26 PM
Yeah... He's probably not much better than my programming teacher...
u/Duke "Tape" Eiyeron 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).
u/James Oldiges 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.
Last Edit: January 14, 2015, 06:58:48 PM by James Oldiges
u/Snektron 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.
u/Dream of Omnimaga 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?
u/JamesV 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 :)
Start a Discussion

b/PC, Mac & Vintage Computers

Computer programming discussion and project showcase

132
Topics
Explore Board
Website statistics


MyCalcs | Ticalc.org | Cemetech | Omnimaga | TI-Basic Developer | MaxCoderz | TI-Story | Casiocalc.org | Casiopeia | The Museum of HP Calculators | HPCalc.org | CnCalc.org | Music 2000 Community | TI Education | Casio Education | HP Calcs | NumWorks | SwissMicros | Sharp Calculators
Powered by EzPortal