Pointers in C: when to use the ampersand and the asterisk?
You have pointers and values:
int* p; // variable p is pointer to integer type
int i; // integer value
You turn a pointer into a value with *
:
int i2 = *p; // integer i2 is assigned with integer value that pointer p is pointing to
You turn a value into a pointer with &
:
int* p2 = &i; // pointer p2 will point to the address of integer i
Edit:
In the case of arrays, they are treated very much like pointers. If you think of them as pointers, you'll be using *
to get at the values inside of them as explained above, but there is also another, more common way using the []
operator:
int a[2]; // array of integers
int i = *a; // the value of the first element of a
int i2 = a[0]; // another way to get the first element
To get the second element:
int a[2]; // array
int i = *(a + 1); // the value of the second element
int i2 = a[1]; // the value of the second element
So the []
indexing operator is a special form of the *
operator, and it works like this:
a[i] == *(a + i); // these two statements are the same thing
Yeah that can be quite complicated since the *
is used for many different purposes in C/C++.
If *
appears in front of an already declared variable/function, it means either that:
- a)
*
gives access to the value of that variable (if the type of that variable is a pointer type, or overloaded the*
operator). - b)
*
has the meaning of the multiply operator, in that case, there has to be another variable to the left of the*
If *
appears in a variable or function declaration it means that that variable is a pointer:
int int_value = 1;
int * int_ptr; //can point to another int variable
int int_array1[10]; //can contain up to 10 int values, basically int_array1 is an pointer as well which points to the first int of the array
//int int_array2[]; //illegal, without initializer list..
int int_array3[] = {1,2,3,4,5}; // these two
int int_array4[5] = {1,2,3,4,5}; // are identical
void func_takes_int_ptr1(int *int_ptr){} // these two are identical
void func_takes_int_ptr2(int int_ptr[]){}// and legal
If &
appears in a variable or function declaration, it generally means that that variable is a reference to a variable of that type.
If &
appears in front of an already declared variable, it returns the address of that variable
Additionally you should know, that when passing an array to a function, you will always have to pass the array size of that array as well, except when the array is something like a 0-terminated cstring (char array).
There is a pattern when dealing with arrays and functions; it's just a little hard to see at first.
When dealing with arrays, it's useful to remember the following: when an array expression appears in most contexts, the type of the expression is implicitly converted from "N-element array of T" to "pointer to T", and its value is set to point to the first element in the array. The exceptions to this rule are when the array expression appears as an operand of either the &
or sizeof
operators, or when it is a string literal being used as an initializer in a declaration.
Thus, when you call a function with an array expression as an argument, the function will receive a pointer, not an array:
int arr[10];
...
foo(arr);
...
void foo(int *arr) { ... }
This is why you don't use the &
operator for arguments corresponding to "%s" in scanf()
:
char str[STRING_LENGTH];
...
scanf("%s", str);
Because of the implicit conversion, scanf()
receives a char *
value that points to the beginning of the str
array. This holds true for any function called with an array expression as an argument (just about any of the str*
functions, *scanf
and *printf
functions, etc.).
In practice, you will probably never call a function with an array expression using the &
operator, as in:
int arr[N];
...
foo(&arr);
void foo(int (*p)[N]) {...}
Such code is not very common; you have to know the size of the array in the function declaration, and the function only works with pointers to arrays of specific sizes (a pointer to a 10-element array of T is a different type than a pointer to a 11-element array of T).
When an array expression appears as an operand to the &
operator, the type of the resulting expression is "pointer to N-element array of T", or T (*)[N]
, which is different from an array of pointers (T *[N]
) and a pointer to the base type (T *
).
When dealing with functions and pointers, the rule to remember is: if you want to change the value of an argument and have it reflected in the calling code, you must pass a pointer to the thing you want to modify. Again, arrays throw a bit of a monkey wrench into the works, but we'll deal with the normal cases first.
Remember that C passes all function arguments by value; the formal parameter receives a copy of the value in the actual parameter, and any changes to the formal parameter are not reflected in the actual parameter. The common example is a swap function:
void swap(int x, int y) { int tmp = x; x = y; y = tmp; }
...
int a = 1, b = 2;
printf("before swap: a = %d, b = %d\n", a, b);
swap(a, b);
printf("after swap: a = %d, b = %d\n", a, b);
You'll get the following output:
before swap: a = 1, b = 2 after swap: a = 1, b = 2
The formal parameters x
and y
are distinct objects from a
and b
, so changes to x
and y
are not reflected in a
and b
. Since we want to modify the values of a
and b
, we must pass pointers to them to the swap function:
void swap(int *x, int *y) {int tmp = *x; *x = *y; *y = tmp; }
...
int a = 1, b = 2;
printf("before swap: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("after swap: a = %d, b = %d\n", a, b);
Now your output will be
before swap: a = 1, b = 2 after swap: a = 2, b = 1
Note that, in the swap function, we don't change the values of x
and y
, but the values of what x
and y
point to. Writing to *x
is different from writing to x
; we're not updating the value in x
itself, we get a location from x
and update the value in that location.
This is equally true if we want to modify a pointer value; if we write
int myFopen(FILE *stream) {stream = fopen("myfile.dat", "r"); }
...
FILE *in;
myFopen(in);
then we're modifying the value of the input parameter stream
, not what stream
points to, so changing stream
has no effect on the value of in
; in order for this to work, we must pass in a pointer to the pointer:
int myFopen(FILE **stream) {*stream = fopen("myFile.dat", "r"); }
...
FILE *in;
myFopen(&in);
Again, arrays throw a bit of a monkey wrench into the works. When you pass an array expression to a function, what the function receives is a pointer. Because of how array subscripting is defined, you can use a subscript operator on a pointer the same way you can use it on an array:
int arr[N];
init(arr, N);
...
void init(int *arr, int N) {size_t i; for (i = 0; i < N; i++) arr[i] = i*i;}
Note that array objects may not be assigned; i.e., you can't do something like
int a[10], b[10];
...
a = b;
so you want to be careful when you're dealing with pointers to arrays; something like
void (int (*foo)[N])
{
...
*foo = ...;
}
won't work.
Put simply
&
means the address-of, you will see that in placeholders for functions to modify the parameter variable as in C, parameter variables are passed by value, using the ampersand means to pass by reference.*
means the dereference of a pointer variable, meaning to get the value of that pointer variable.
int foo(int *x){
*x++;
}
int main(int argc, char **argv){
int y = 5;
foo(&y); // Now y is incremented and in scope here
printf("value of y = %d\n", y); // output is 6
/* ... */
}
The above example illustrates how to call a function foo
by using pass-by-reference, compare with this
int foo(int x){
x++;
}
int main(int argc, char **argv){
int y = 5;
foo(y); // Now y is still 5
printf("value of y = %d\n", y); // output is 5
/* ... */
}
Here's an illustration of using a dereference
int main(int argc, char **argv){
int y = 5;
int *p = NULL;
p = &y;
printf("value of *p = %d\n", *p); // output is 5
}
The above illustrates how we got the address-of y
and assigned it to the pointer variable p
. Then we dereference p
by attaching the *
to the front of it to obtain the value of p
, i.e. *p
.