Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Determine the Output of this Code in C


Scar0ptics's Avatar
Member
0 0

Run this through a Linux compiler using g++ and play around with the values within the array. I have posted the solutions, but I did not run this through a compiler, so there might be errors.

#include <stdio.h>

int main(){ int x [5]; x[0] = 10; x[1] = 30; x[2] = 50; x[3] = 70; x[4] = 99; int *y = x;

printf("%d\n", *y+2); // 12 printf("%d\n", *(y+2)); // 50 printf("%d\n", x[5]); // writes random number from system memory. printf("%d\n", y); // writes random number from system memory.

}


_spartax_'s Avatar
Member
0 0

the stack layout is

HIGH MEMORY x[4] x[3] x[2] x[1] x[0] y == &x[0] LOW MEMORY


Scar0ptics's Avatar
Member
0 0

Huitzilopochtli wrote: Is answer Spag ?

Damn it Bobby, raise your hand. We need to ask Mordak if we can add a section for all the SPAG folks at HBH tee-hee:P

Thanks for sharing that spartax