Pointer Variables and Address Operator in C

Pointer Variables and Address Operator in C

In this class, we will understand Pointer Variables and Address Operator in C.

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.

Pointer Variables and Address Operator in C Declaration
Pointer Variables and Address Operator in C Declaration

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.

Pointer Variables and Address Operator in C integer memory
Pointer Variables and Address Operator in C integer 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.

Pointer Variables and Address Operator in C initialization
Pointer Variables and Address Operator in C initialization

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.

Pointer Variables and Address Operator in C floating point declaration
Pointer Variables and Address Operator in C floating point declaration

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.

Pointer Variables and Address Operator in C Assignment
Pointer Variables and Address Operator in C Assignment