3 ms·
The only reason it doesn't work is because the stack grows downwards on x86 targets so p is at a higher address than q. If you swap the variables round, it comp
by James_K 2y ago
The only reason it doesn't work is because the stack grows downwards on x86 targets so p is at a higher address than q. If you swap the variables round, it compiles and runs printing the value assigned.
https://godbolt.org/z/bedb7cvY8 https://godbolt.org/z/bedb7cvY8
- shakna 2y agoAs the compiler is free to reorganise the variable order, that means you're relying on Undefined Behaviour for it to compile and do what you want. That's not something inherent to the way pointers work.
- James_K 2y agoIt is valid C program. Undefined behaviour occurs at runtime, but it is valid and should be compiled by a conformant C compiler.
- spaqin 2y ago[dead]