I am learning C. I was writing code to create a linked list when i came across a segmentation fault. I found a solution to my problem in this question. I was trying to pass a pointer by reference. The solution says that we can't do so. We have to pass a pointer to a pointer. This solution worked for me. However, I don't understand why is it so. Can anyone tell the reason?
Asked
Active
Viewed 4,811 times
5
-
12Because there are no references in C. Do you need reason for why there are no references in C? – Jun 05 '14 at 05:28
-
2The language doesn't support passing objects by reference. Simple as that. – R Sahu Jun 05 '14 at 05:28
-
You *can* pass pointers to pointers, though, but they're still passed by value. – David Ehrmann Jun 05 '14 at 05:30
-
6In C *everything* is pass-by-value (I can almost smell the flame jets warming up with that statement). If you want to have a caller-side modifiable parameter, the "value" you pass must be the address of the thing-to-modify, and the formal parameter must be declared as a pointer to that type (even if its a pointer, in which case it would be a pointer-to-pointer). Even arrays are pass-by-value, it just happens the "value" per the standard for an array as an expression is the address of its first element. But its still a *value* (one that give you access to hit up the callers var, admittedly). – WhozCraig Jun 05 '14 at 05:31
-
2Perhaps this post will also help you to understand: http://stackoverflow.com/questions/23963269/can-someone-explain-how-pointer-to-pointer-works/23964156#23964156 Basically, in order to manipulate where a pointer points, you must have it's address. The address of an object -is- a pointer to an object. Hence, passing the address of a pointer (ie: a pointer to a pointer) will allow the called function to change where the pointer points. – Mahonri Moriancumer Jun 05 '14 at 05:33
-
1In C++ a "reference" is also a pointer, its just not visible to the outside. So if you pass a pointer in C (or a pointer to a pointer in your case) it's the same code as in C++ passing a rerfernce, only the syntax differs. – Devolus Jun 05 '14 at 05:33
-
And internally the only difference between a pointer and a reference in C++ is that the pointer can be dangling, can be NULL, but a reference always, well, "refers" to an existing object. – LaszloLadanyi Jun 05 '14 at 05:47
-
1@LaszloLadanyi Really? Can't I dereference a `NULL` or dangling pointer and pass that as a reference? The effect would be the same... – glglgl Jun 05 '14 at 06:22
-
I stand corrected. This code compiles, and segfaults... ` int main() { int *pi = 0; int &ri = *pi; ri=5; return 0; } ` – LaszloLadanyi Jun 05 '14 at 06:53
1 Answers
9
From The C Programming Language - Second Edition (K&R 2):
5.2 Pointers and Function Arguments
Since C passes arguments to functions by value, there is no direct way for the called function to alter a variable in the calling function.
...
Pointer arguments enable a function to access and change objects in the function that called it.
If you understand that:
void fn1(int x) {
x = 5; /* a in main is not affected */
}
void fn2(int *x) {
*x = 5; /* a in main is affected */
}
int main(void) {
int a;
fn1(a);
fn2(&a);
return 0;
}
for the same reason:
void fn1(element *x) {
x = malloc(sizeof(element)); /* a in main is not affected */
}
void fn2(element **x) {
*x = malloc(sizeof(element)); /* a in main is affected */
}
int main(void) {
element *a;
fn1(a);
fn2(&a);
return 0;
}
As you can see, there is no difference between an int
and a pointer to element, in the first example you need to pass a pointer to int
, in the second one you need to pass a pointer to pointer to element.

David Ranieri
- 39,972
- 7
- 52
- 94
-
You seem to be quoting some source (start of you answer). Would you mind mentioning this source? – alk Jun 06 '14 at 05:59