![]() |
VOOZH | about |
A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. It is the backbone of low-level memory manipulation in C.
0x7fffffffe9cc
This hexadecimal integer (starting with 0x) is the memory address.
👁 ImageLet us understand different steps of the above program.
We have to first dereference the pointer to access the value present at the memory address. This is done with the help of dereferencing operator(*) (same operator used in declaration).
10
Note: Earlier, we used %d for printing pointers, but C provides a separate format specifier%p for printing pointers.
The size of a pointer in C depends on the architecture (bit system) of the machine, not the data type it points to.
The size remains constant regardless of the data type (int*, char*, float*, etc.). We can verify this using the sizeof operator.
8 8
The reason for the same size is that the pointers store the memory addresses, no matter what type they are. As the space required to store the addresses of the different memory locations is the same, the memory required by one pointer type will be equal to the memory required by other pointer types.
Note: The actual size of the pointer may vary depending on the compiler and system architecture, but it is always uniform across all data types on the same system.
There are 4 special types of pointers that used or referred to in different contexts:
The wild pointers are pointers that have not been initialized with something yet. These types of C-pointers can cause problems in our programs and can eventually cause them to crash. If values are updated using wild pointers, they could cause data abort or data corruption.
Memory freed
The pointer arithmetic refers to the arithmetic operations that can be performed on a pointer. It is slightly different from the ones that we generally use for mathematical calculations as only a limited set of operations can be performed on pointers. These operations include:
In constant pointers, the memory address stored inside the pointer is constant and cannot be modified once it is defined. It will always point to the same memory address.
Output
solution.c: In function ‘main’:
solution.c:11:9: error: assignment of read-only variable ‘ptr’
11 | ptr = &b;
| ^
As we try here to assign ptr a new address, the compiler throws an error because a constant pointer cannot change the memory location it was originally assigned.
We can also create a pointer to constant or even constant pointer to constant. Refer to this article to know more - Constant pointer, Pointers to Constant and Constant Pointers to Constant
A function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs.
15
In C, we can create multi-level pointerswith any number of levels such as – ***ptr3, ****ptr4, ******ptr5 and so on. Most popular of them is double pointer (pointer to pointer). It stores the memory address of another pointer. Instead of pointing to a data value, they point to another pointer.
var: 10 *ptr1: 10 **ptr2: 10
Following are the major advantages of pointers in C:
Pointers are vulnerable to errors and have following disadvantages:
Read about Difference between Pointers and Arrays.