In programming, there are various tasks that can be achieved easily with the use of pointers such as dynamic memory allocation that needs pointers to perform. It is important to learn pointers for the programmer because it minimizes the execution time and saves memory space. Before we jump into the Pointers let us understand a little bit about the Memory Addresses.
Memory Address:
As we already know at this point that every variables declared in a program are stored in computer storage possessing some memory address. And these addresses can be accessed by using the ampersand (&) operator. If you have the variable name var then &var
is used to access the address of that variable in a program.
Consider the following program in C:
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h> int main() { int var = 10; //Displaying the address of Variable var printf("Address of var: %p", &var); return 0; } |
Output:
1 2 3 | Address of var: 0x7ffeeca6915c //You might get the different address |
What is Pointer?
In C programming language pointer is a variable that stores or points to the address of another variable. This variable can be of type int, char, array, function, or any other pointer. It does not stores the value but only the address of the pointed variable.
Syntax of Pointer:
Pointer variables are declared in the following way using asterisk * symbol ( asterisk * = same asterisk used for multiplication).
1 | data_type *pointer_name; |
You can declare the pointer with different C data-types too, some are:
1 2 3 | int *intp; //pointer to an integer float *fltp; //pointer to a float char *chp //pointer to a character |
Initialization of Pointers:
After declaring the pointer, we need to initialize the pointers. To get the actual address we use the ampersand (&) operator and it is used before the variable name that pointer needed to be the point.
1 | pointer = &variable_name; |
Short example to use in a program:
1 2 3 4 5 | int *ptr; int x =10; x = 10; ptr= &x; |
- Reference operator (
&
): It returns the variable’s address. - Dereference operator (
*
): It gets the value that has been stored in a memory address.
Size of Pointers in C.
The size of pointer is same for all the variable irrespective of their data types. However it depends on your machine architecture. For example, for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes.
See the example and you can check it on your computer by running the following program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <stdio.h> int main() { int *ptrI; //integer Pointer float *ptrF; //float Pointer char *PtrC; //character Pointer //Size of the pointer respectively printf("The size of int pointer is: %d\n",sizeof(ptrI)); printf("The size of float pointer is: %d\n",sizeof(ptrF)); printf("The size of char pointer is: %d\n",sizeof(PtrC)); return 0; } |
Output:
1 2 3 | The size of int pointer is: 8 The size of float pointer is: 8 The size of char pointer is: 8 |
Example: Use of pointer in C Program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> int main () { int num = 10; int *ptr; //Declaring Pointer ptr = # //initializing pointer //Address stored in a ptr (address of num) printf("Address of num variable: %p\n", ptr); // Displying actual value present in that address printf("Value of ptr: %d\n", *ptr ); return 0; } |
Output:
1 2 | Address of num variable: 0x7ffebfe68824 Value of ptr: 10 |
As you can see, in the example above we printed two things:
- We printed
ptr
that had already stored the address of num (pointed by this line:ptr = #
) and so the program displayed the address onnum
variable. - We printed
*ptr
that displayed the actual value of the variable(num) that the address stored in ptr points to.
Now in a simple way, we can say that asterisk *
returns the value of the referenced variable and ampersand (&
) operator returns the address of a variable.
NULL Pointers
When a pointer is assigned with a null value at the time declaration then it is called a NULL pointer. We use this value when we do not assign any address to the pointer. The null pointer always contains a constant value of 0.
Let us see an C example for NULL pointer:
1 2 3 4 5 6 7 8 9 | #include <stdio.h> int main() { int *ptr = NULL; //null pointer printf(“The value inside ptr: %x”, ptr); return 0; } |
Output:
1 | The value inside ptr: 0 |
Advantages and Disadvantages of pointers
Pointers allow memory allocation that is Dynamic memory allocation as well as deallocation.
They are useful for complex data structures such as in a linked list, graph, tree, etc.
They are complex to understand, fault value may cause memory corruption pointers are comparatively slower than the variable.