Pointer to array in C Programming Language

pointer-to-array-in-c-language-explanation

Introduction:

In an earlier article, we discussed the relationship between pointers and arrays. In today’s article, We will look at the pointer to array in c programming language.

📢 Recommended: Pointers Practice Programs series

What is Pointer to Array in C Language:

The C programming language allows us to declare a pointer that points to the entire array. This type of pointer is called Pointer to Array.

In earlier posts, we looked at the pointer to the array which points to the first element of the array.

In the above example, The pointer variable ptr is pointing to the single element of the files array, that is the first element of the array ( 0th Index - files[0] element).

So if you increment the above pointer ptr, It will go to the next element in the array (i.e 1st Index - files[1] element).

Similarly, We can create a pointer variable, that points to the entire array instead of a single element. Let’s look at the syntax of the pointer to array in c.

Syntax of Pointer to Array in C:

data_type (*pointer_name) [size];

Here

  • data_type is the pointer datatype.
  • pointer_name is the name of the pointer
  • size is the size of the array that the pointer going to point to.

The above pointer( pointer_name) can store the address of the array of data_type and size of size.

Let’s look at the example of Pointer to Array in C

Example of Pointer to Array in C Language:

Create an integer array called shares with the size 5.

Now let’s create a pointer to an array called arrPtr.

The arrPtr pointer to an array will point to the entire array of shares[5] (unlike simple pointers). So the arrPtr can store the address of an integer array with 5 elements.

The pointer arithmetic is performed based on the base type of the pointer variable.

So If we increment the pointer to an array( arrPtr), The pointer to an array( arrPtr ) value will be incremented by 5 * Integer Size bytes. ( As the base type of arrPtr is Array of 5 integers)

📢The pointer to an array is used while working with multidimensional arrays.

Let’s look at an example program to understand the pointer to an array in C Programming Langauge.

Program to Understand Pointer to an Array in C Programming Language:

In the following program, We are going to declare an array( shares) with the size of 5 Integers. Then we will create two pointer variables, normalPtr and arrPtr.

The normalPtr is an integer pointer, which points to the 0th index of the shares array.

The arrPtr is a Pointer to an array of 5 integers ( int (*arrPtr)[5] ) and points to the entire array of shares.

To demonstrate that the arrPtr is pointing to the entire shares array, We perform the pointer increment operation on the above two pointers and display the results.

Program Output:

Let’s compile and run the program. We are using the GCC compiler to compile the C program in this example.

pointer-to-array-in-c-programming-with-example-programs

As you can see from the above output, Initially both the normal pointer normalPtr and the pointer to an array ( arrPtr) are pointing to the base address of the array. i.e 0x7ffdd2825cc0

Once we incremented the normal pointer( normalPtr) and the pointer to an array( arrPtr), The values of the pointers are changed.

The normalPtr value is increased by 4 Bytes ( Assuming the size of the integer is 4 Bytes)

But the value of the pointer to an array arrPtr is increased by 20 Bytes. which is equal to the size of the entire shares array ( 5 * 4 Bytes).

📢If we increment the pointer to an array, The value of the pointer to an array will be increased by the size of the entire array.

<strong>Before Increment - arrPtr - 0x7ffdd2825cc0</strong>

arrPtr++;

After Increment - arrPtr - 0x7ffdd2825cd4

Difference – 20 Bytes

📢The Pointers to an array are used extensively to traverse the multidimensional arrays. As we can simply move from one array to another array by performing the arithmetic operations on the pointer to an array.

Dereferencing the Pointer to an Array in C Programming Language.

The normal pointers are pointing to the first element of the array ( zeroth index – shares[0]), If we dereference the pointer, We will get the value of the 0th index element in the array. ( shares[0])

Similarly, The pointer to an array( arrPtr) is pointing to the entire array, so if we dereference the pointer to an array, We will get the array.

Let’s look at a program to demonstrate the dereference operator on a pointer to an array in C Programming language.

The normalPtr and arrPtr are storing the address of the shares array. while the normalPtr is a normal integer pointer, The arrPtr is a pointer to an array.

Program Output:

Compile the program.

$ gcc pointer-to-array-derefence.c

Run the program.

dereferencing-a-pointer-to-an-array-in-c-language

As we can see from the above program output,

  • Dereferencing the normal pointer *normalPtr returned the value of the 0th index element of the shares array. that is equal to 40.
  • Similarly, If we apply the dereference operator on the pointer to an array *arrPtr, We got the address of the entire array. That is equal to 0x7ffebc06d380 ( value of *arrPtr = 0x7ffebc06d380)

We can further confirm that we got the entire array by using the sizeof() operator on the above-dereferenced values. ( Example : sizeof(*arrPtr))

Pointers with 2D Arrays in C Language :

The Pointers to an array are used extensively to traverse the multidimensional arrays. As we can simply move from one array to another array by performing the arithmetic operations on the pointer to an array.

📢Do note that, Each element of the Two Dimensional arrays (2D array) is a one-dimensional array.

Let’s look at a program to traverse a Two-dimensional array using a pointer to an array.

Pointers to an array to access the elements of the 2 Dimensional Array in C Programming Language:

In the following program, We have created a 2-Dimensional array (2D Array) called arr with the 3 rows and 3 columns and initialized it with the values.

Then we created a pointer to an array called arrPtr, The arrPtris a pointer to an array of 3 Integer elements. and initialized the pointer to an array arrPtrwith the 2D Array arr.

Finally, We traversed the 2D array using the pointer to an array arrPtr and displayed the elements of the 2D array by dereferencing the pointer to an array arrPtr

Here is the program to access the values of the 2D Array using a pointer to an array in C Programming language.

Note, that we used the pointer expression *(*(arrPtr+i)+j)to print the values of the 2D array. As we are using the 2-Dimentional array, we need to dereference twice to get the values.

We can simply use the subscript notation twice like below to get the values of the 2 Dimensional array.

Program Output:

Let’s compile and Run the program.

access-2d-array-elements-using-pointer-to-an-array-in-c-language

As we can see from the above output, We are able to access the elements of the 2D Array arr using the pointer to an array arrPtr.

📢Similarly, We can use the pointer to arrays to access the elements of the 3-Dimentional arrays as well.

Conclusion:

We looked at the Pointer to array in C programming language, How to Dereferencing the Pointer to an Array. We also looked at the Pointers with 2-Dimentional arrays and How to store and access the 2D Arrays using the Pointer to an entire array in C language.

Related C Programming Tutorials:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

Leave a Reply