# Understanding Pointer to Array in C++ With Examples [Latest] ### Introduction

Today in this tutorial, we are going to understand the concept of the Pointer to Array in C++.

Before we head directly to the topic, we first would like to guide the readers through the pointer arithmetics in C++. It is going to help understand the concept in a better way.

## Pointer Arithmetics

Basically, pointers are variables that store the address of any other variable of a specific data type.

Let us go through a simple example to get a clear idea about the pointer arithmetics.

Output:

Here,

• We first initialize two int and float variables i and j respectively with some values,
• Then we store the address of the two variables inside two pointers `x` and `y` in lines 11 & 12, and print them out,
• After that, we increment both the pointers and see the change in output.

Observe the 5th and 6th lines of the output. 7339520 is original value in x plus 4, and 7339516 is original value in y plus 4. This so happens because every time a pointer is incremented it points to the immediately next location of its type.

That is why, when the integer pointer `x` is incremented, it points to an address two locations after the current location since an integer is always 4 bytes long (We checked that using the sizeof() function above).

Similarly, `y` points to an address 4 locations after the current location. This is a very important result and can be effectively used while passing the entire array to a function.

Note: Do not attempt the following operations on pointers, they would never work,

• Multiplication of a pointer with a constant,
• Division of a pointer with a constant.

## Pointers And Arrays in C++

Now, let us take a deep look at accessing array elements using pointers.

Output:

In the code above,

• At first, we initialize an array of size 4 and a pointer for the same type,
• After that, we assign the base address or the address of the 1st element of the array to the pointer(Since, `arr` and `&arr` are same), `ptr`,
• Then we print out the elements and their corresponding addresses using pointers.

As we discussed in the previous section, `(ptr+i)` is the address of the ith element from the array arr. And `*(ptr+i)` actually refers to the value at the address (ptr+i). Therefore, it gives us the value of the ith element.

## Passing An Array to Function Using Pointers

Let us see how we can pass a whole array to a function with the help of pointers as an example, to get a clear understanding of the topic.

Output:

Similar to the previous example, here we initialize an array inside the `main()` and pass the base address arr and the array size as parameters to the `show()` function we defined before.

After the call of the show() function, at this point, the pointer ptr stores the base address of the array(arr) and n is the size. Further, we use a `for` loop to print the whole array using the concept of a pointer to array.

## Conclusion

So, in this tutorial, we learned the working as well as the use of the Pointers to Arrays in C++. Practicing the above codes/examples would help understand the topic further.

For any questions, feel free to use the comments below.