Pointer Variables and Address Operator in C
In this class, we will understand Pointer Variables and Address Operator in C.
Table of Contents
Pointer Variables and Address Operator
Pointer Variable
So far, we have covered different variables like int, float, double, char, and many more.
Now, we will try to understand a new variable called the pointer variable.
The pointer variable is a variable capable of storing the address of another variable.
The image shows the declaration of the pointer variable.
The star operator in the declaration states that the variable is a pointer variable.
Address Operator
The image below shows the address at which the variable has been stored in the memory.
The above image shows that the variable a has been allotted the starting address 2000, and int is taking 2 bytes of space.
The image below is the initialization of the pointer variable.
So far, we haven’t used the ampersand operator in any of our initialization.
In the pointer variable initialization, we have to use the ampersand symbol.
The use of the ampersand symbol will fetch the starting address of the variable.
The starting address fetched will be stored in the pointer variable.
Now pointer variable will get stored with address 2000.
Let’s try to understand the use of int in the pointer variable initialization.
The int *p means p is a pointer variable capable of storing the address of an integer variable.
What if we want to store the address of a floating-point variable.
The image below shows the declaration to store the address of a floating-point variable.
In the above declaration, b is a floating-point variable, and fp is a pointer variable capable of storing the address of a floating-point variable.
In our case, the address of floating-point variable b is getting stored in fp.
Assigning Pointer Variables
We have seen how to initialize a pointer variable.
The image below shows how to assign a pointer variable.