In this C tutorial we will look at some specifics of pointers.
Initialize a pointer
Before you can use a pointer in for instance a printf statement, you have to initialize the pointer.
The following example will not initialize the pointer:
The following example will not initialize the pointer:
#include<stdio.h>
int main(void) {
int *ptr_p;
printf("%d\n",*ptr_p);
return 0;
}
In this example we print the value that ptr_p points to. However, we did not initialize the pointer. In this case the pointer contains a random address or 0.
The result of this program is a segmentation fault, some other run-time error or the random address is printed.
The meaning of a segmentation fault is that you have used a pointer that points to an invalid address. In most cases, a pointer that is not initialized or a wrong pointer address is the cause of segmentation faults. The next example demonstrates the correct usage of pointers:
The meaning of a segmentation fault is that you have used a pointer that points to an invalid address. In most cases, a pointer that is not initialized or a wrong pointer address is the cause of segmentation faults. The next example demonstrates the correct usage of pointers:
#include<stdio.h>
int main(void) {
int x;
int *ptr_p;
x = 5;
ptr_p = &x;
printf("%d\n", *ptr_p);
return 0;
}
Note: If you forget to place * (in front of the pointer) in the printf statement, you will print the address of integer x. (Try it!).
Pointers as function arguments
The C language is a “call by value” language, which means that the called function is given a copy of its arguments, and doesn’t know their addresses. (For example myfunction(x) call is given, the value of x is passed, not its address). This makes it impossible to change the value of x from the inside of the function (myfunction).
Note: With an array this is not a problem. If x is an array (char x[10]) then x is is an address anyway.
Take a look at the following example, which will illustrate the problem:
#include<stdio.h>
void swapping(int c, int d) {
int tmp;
tmp = c;
c = d;
d = tmp;
printf("In function: %d %d\n", c , d);
}
int main(void) {
int a,b;
a=5;
b=10;
printf("input: %d %d\n", a, b);
swapping(a,b);
printf("output: %d %d\n", a, b);
return 0;
}
In the example the values of the parameters are swapped in the function swapping. But when the function returns nothing will happen. The result is that the values are not swapped. (Try it!).
Pointers can be used to get around the “call by value” restriction. In the next example we will use pointers to correct the problem:
#include<stdio.h>
void swapping(int *ptr_c, int *ptr_d) {
int tmp;
tmp = *ptr_c;
*ptr_c = *ptr_d;
*ptr_d = tmp;
printf("In function: %d %d\n", *ptr_c , *ptr_d);
}
int main(void) {
int a,b;
a=5;
b=10;
printf("input: %d %d\n", a, b);
swapping(&a,&b);
printf("output: %d %d\n", a, b);
return 0;
}
Note: Don’t forget to replace “swapping(a,b);” for swapping(&a,&b);”.
Pointing to the same address
There is no limit on the number of pointers that can point to the same address.
#include<stdio.h>
int main() {
int a;
int *ptr_b , *ptr_c, *ptr_d;
ptr_b = &a;
ptr_c = &a;
ptr_d = ptr_b;
return 0;
}