A pointer is a variable that stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.
The size of the pointer depends on the architecture. However, in 32-bit architecture the size of a pointer is 2 byte.
Consider the following example to define a pointer which stores the address of an integer.
int n = 10; int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.
A pointer is defined like a normal variable, but with an asterisk(*) before the variable name. It is also known as indirection pointer used to dereference a pointer.
int *a;//pointer to int char *c;//pointer to char
The above declaration means that "a" is a pointer to a variable of type int and "c" is a pointer to a variable of type char.
In the below example, using pointers we print the address and value.
In the above example, pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.
With the help of * (indirection operator), we can print the value of pointer variable p.
Let's see the pointer example as explained for the above figure.
#includeint main(){ int number=50; int *p; p=&number;//stores the address of number variable printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number. printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p. return 0; }
Sometimes we might want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer.
int *ptr[MAX];
int arr[10]; int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.
Functions can also be represented with a pointer. A function pointer is defined in the same way as a function prototype, but the function name is replaced by the pointer name prefixed with an asterisk and encapsulated with parenthesis. Such as
int (*fptr)(int, char); fptr=some_function;
To call this function:
(*ftpr)(3,'A');
This is equivalent to:
some_function(3,'A');
void show (int); void(*p)(int) = &display; // Pointer p is pointing to the address of a function
A structure or union can have a pointer to represent it. Such as:
struct some_structure homer; struct some_structure *homer_pointer; homer_pointer=&homer;
This defines homer_pointer to point to the structure homer. Now, when you use the pointer to reference something in the structure, the record selector now becomes -> instead of a period.
homer_pointer->an_element=5;
This is the same as:
homer.an_element=5;
struct st { int i; float f; }ref; struct st *p = &ref;
The void pointer can represent an unknown pointer type.
void *joe;
This is a pointer to an undetermined type.
There are many applications of pointers in c language.
The address of operator '&' returns the address of a variable. But, we need to use %u to display the address of a variable.
#includeint main(){ int number=50; printf("value of number is %d, address of number is %u",number,&number); return 0; }
A pointer that is not assigned any value but NULL is known as the NULL pointer. If you don't have any address to be specified in the pointer at the time of declaration, you can assign NULL value. It will provide a better approach.
In the most libraries, the value of the pointer is 0 (zero).
Pointer Program to swap two numbers without using the 3rd variable.
#includeint main(){ int a=10,b=20,*p1=&a,*p2=&b; printf("Before swap: *p1=%d *p2=%d",*p1,*p2); *p1=*p1+*p2; *p2=*p1-*p2; *p1=*p1-*p2; printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2); return 0; }
Below are the precedence and associativity of the operators which are used regarding pointers. There are While reading the complex pointers, several things must be taken into the consideration.
Operator | Precedence | Associativity |
---|---|---|
(), [] | 1 | Left to right |
*, identifier | 2 | Right to left |
Data type | 3 | - |
Here,we must notice that,
How to read the pointer: int (*p)[10].
To read the pointer, we must see that () and [] have the equal precedence. Therefore, their associativity must be considered here. The associativity is left to right, so the priority goes to ().
Inside the bracket (), pointer operator * and pointer name (identifier) p have the same precedence. Therefore, their associativity must be considered here which is right to left, so the priority goes to p, and the second priority goes to *.
Assign the 3rd priority to [] since the data type has the last precedence. Therefore the pointer will look like following.
The pointer will be read as p is a pointer to an array of integers of size 10.
Another example to read the following pointer.
int (*p)(int (*)[2], int (*)void))
This pointer will be read as p is a pointer to such function which accepts the first parameter as the pointer to a one-dimensional array of integers of size two and the second parameter as the pointer to a function which parameter is void and return type is the integer.