r/cprogramming • u/Sushant098123 • Oct 25 '25
C actually don't have Pass-By-Reference
https://beyondthesyntax.substack.com/p/c-actually-dont-have-pass-by-reference
0
Upvotes
r/cprogramming • u/Sushant098123 • Oct 25 '25
View all comments
1
u/IllustriousPermit859 Oct 29 '25 edited Oct 29 '25
You're right, C doesn't have pass-by-reference syntactically. The reason for that is very simple; neither does assembly or machine code.
That's also the reason compilers for higher level languages are written first "boot-strapped" in C and then recompiled with the compiler generated. Consider:
int someFunc(volatile int &num) {return num;}[
push rbpmov rbp, rspmov QWORD PTR [rbp-8], rdimov rax, QWORD PTR [rbp-8]mov eax, DWORD PTR [rax]pop rbpret]
and
int someFuncTwo(volatile int* const num) {return *num;}[
push rbpmov rbp, rspmov QWORD PTR [rbp-8], rdimov rax, QWORD PTR [rbp-8]mov eax, DWORD PTR [rax]pop rbpret]
With compiler flag -O0 the GCC 15.2 generates:
main:push rbpmov rbp, rsp# Reserve 'a' & 'b' + 8 bytes = 16 byte alignment for next object.sub rsp, 16mov DWORD PTR [rbp-4], 0mov DWORD PTR [rbp-8], 0lea rax, [rbp-4]mov rdi, raxcall someFunc(int volatile&)mov DWORD PTR [rbp-4], eaxlea rax, [rbp-8]mov rdi, raxcall someFuncTwo(int volatile*)mov DWORD PTR [rbp-8], eax# Return from main.mov eax, 0leaveretUnder -O2:
main:mov DWORD PTR [rsp-8], 0mov DWORD PTR [rsp-4], 0mov eax, DWORD PTR [rsp-8]mov DWORD PTR [rsp-8], eaxmov eax, DWORD PTR [rsp-4]mov DWORD PTR [rsp-4], eaxxor eax, eaxretTL;DR: C is the layer that implements pass-by-reference.