What is a Pointer to Array in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The pointer in C++ language is a variable. It is also known as a locator or indicator that points to an address of a variable or memory block. Pointers can store the address of a single variable, as well as it can also store the address of indexes of an array.

In C++, if the array name is assigned to a pointer, then the pointer points to the first element of the array.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Example

Here, in the above code, we have initialized a pointer variable pttr of int datatype, and after that, we have initialized an array of int datatype of size 5. After that, we assigned the array to pttr, which means that the address of the first element of the array is stored in pttr.

Similarly, we can point to the first element of the array using &array_name[0].

Similarly, we can retrieve the address of the rest of the elements by array + 1 and array + 2.

What are Pointers to Multi-Dimensional Arrays in C++?

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Pointers and Two-dimensional Arrays

In a 2-D array, we can access any array element using 2 subscripts, where the first subscript describes row number and the second subscript describes column number.

We can also access the element of the array using pointer notation. Suppose an array is a 2-D array. We can access any element array[i][j] of the array using the pointer expression *(*(array+i)+j).

Let's take a 2-D array to understand the concept:

Output

Since in the memory, the array is stored linearly. It is not stored as a 2-D table, as shown above. The concept of rows and columns is only theoretical. The array is stored in row-major order, i.e., rows are placed next to each other.

A two-dimensional array can be considered a collection of one-dimensional arrays arranged one after the other, each row being a 1-D array. To put it another way, 2-D dimensional arrays are arranged sequentially. This means that "arr" is an array of two elements, each of which is a 1-D array of three integers.

Code:

Output:

Explanation:

In the above code, we have initialized an array with the elements. After that, we have initialized a pointer named pttr with the array. After that, in the next line, we used the pointer expression to print the elements of the array, and after that, we used the subscript expression to print the elements of the array.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Pointers and Three-dimensional Arrays

In a 3-D array, we can access any array element using 3 subscripts.

We can define the 3-D array as a 2-D array, i.e., each element in the 3-D array is considered a 2-D array. Similarly, the pointer expression for the 3-D array is *(*(*(arr + i ) + j ) + k) and the subscript expression for it is arr[i][j][k].

Code:

Output:

Explanation:

In the above code, we have declared a 3-D array. After that, we declared three variables named i, j, and k for iterating through the loops. In the nested for loop, we have printed the array elements using pointer notation.

Subscripting Pointer to an Array

Suppose arr is a 2-D array with 2 rows and 3 columns and ptr is a pointer to an array of 3 integers, and pttr contains the base address of array arr.

Since pttr is a pointer to an array of 3 integers, pttr + i will point to ith row. On dereferencing pttr + i, we get the base address of the ith row. To access the address of the jth element of the ith row, we can add j to the pointer expression *(pttr + i). So the pointer expression *(pttr + i) + j gives the address of the jth element of the ith row, and the pointer expression *(*(pttr + i)+j) gives the value of the jth element of an ith row.

We know that the pointer expression *(*(pttr + i) + j) is equivalent to the subscript expression pttr[i][j]. So if we have a pointer variable containing the base address of the 2-D array, then we can access the elements of an array by double subscribing to that pointer variable.

Let's take a code to understand the concept of pointer to array:

Output:

Explanation:

In the above, we have used a pttr pointer variable pointing to the array's base address. After that, using pointer_name+index, we printed the address of the element of the array. Similarly, after that, we used pointer and subscript expressions to display the element of the array

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Let's take a code in which we will print the array elements using a pointer to array notation.

Code:

Output:

Explanation:

In the above code, we made an array data structure of size 5. After that, we inserted 5 numbers from the user, and then we printed the array elements using array+1, array+2, and so on.

Conclusion

  • Pointers can store the address of a single variable as well as it can also store the address of indexes of an array.
  • We can get the array's first element by assigning the array's name to the pointer.
  • Similarly &array_name[0] gives us the first element of the array.
  • we can access the elements of 2-D and 3-D arrays using pointer notation.
  • We can display the elements of the array using subscript notation.