Pointers and Arrays in C

In this class, we will understand Pointers and Arrays in C.

Pointers and Arrays in C

So far, we have seen how the integer and floating-point variables address can be assigned to pointer variables.

But C provides the facility to assign the address of arrays also.

Pointers and Arrays

To understand this concept, we will use the program shown in the image below.

Pointers and Arrays in C Programming Example
Pointers and Arrays in C Programming Example

We will try to visualize every line of code and understand how we can assign an array to a pointer.

In the first line of code, an integer array of five elements has been created.

Assume that each integer occupies two bytes of space.

Pointers and Arrays in C Programming First Line
First Line

A pointer variable p has been created in the second line of code, but the pointer variable has not been assigned.

Pointers and Arrays in C Programming Second Line
Second Line

In the third line of code, we assign the starting address of the array’s first element to the pointer variable.

Pointers and Arrays in C Programming Third Line
Third Line

In the fourth line of code, we assign five to the array’s first element by using the pointer variable.

Pointers and Arrays in C Programming Fourth Line
Fourth Line

In our next class we will try to understand How to access the values of an array using pointer variable.