3 ms·
As a C program, it doesn't actually work. [0] It's just C-as-pseudocode for LLVM IR. And LLVM has different semantics for what is allowed and denied, than C. [
by shakna 2y ago
As a C program, it doesn't actually work. [0] It's just C-as-pseudocode for LLVM IR. And LLVM has different semantics for what is allowed and denied, than C.
[0] https://godbolt.org/z/58GcP1d9j https://godbolt.org/z/58GcP1d9j
- James_K 2y agoThe 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]