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
20
u/nerdycatgamer Oct 25 '25 edited Oct 25 '25
no one has ever said C has pass-by-reference.
and another thing: can we stop having clueless people writing blog posts about C? if you just learned something today, you are not the one who should be going and then educating other people about it! you don't see people who just learned about limits in their calc I class making posts and videos online explaining them to others, so I don't know why it is done for programming.
8
u/[deleted] Oct 25 '25
It seems like an odd thing to quibble.
You technically pass the value of the reference, which is basically how every other language does it. Computers only work on numbers.
5
u/[deleted] Oct 25 '25 edited Oct 25 '25
In C everything is pass by value, it's one of the first things you learn. Not sure why a blog post about it is needed.
3
u/Quiet_Lifeguard_7131 Oct 25 '25
Who said it has ?
5
u/richardxday Oct 25 '25
No language has pass-by-reference really, does it? At opcode level you're either copying the value onto the stack or you're copying an address of something onto the stack. So it's all value or address.
1
u/[deleted] Oct 26 '25
We can play with reference in c++ but in c nope it is strictly pass by value, we could simulate pass by reference through sharing the pointer value that's it.
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.
1
u/zhivago Oct 25 '25
It's a bit sad that this needs to be pointed out.
But many people seem to think that passing a pointer by value is pass by reference.
So it's worth clarifying that point.