Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Pointer to Arrays in Objective-C
Next article icon

Pointer to an Array in C++

Last Updated : 07 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Pointers in C++ are variables that store the address of another variable while arrays are the data structure that stores the data in contiguous memory locations. In C++, we can manipulate arrays by using pointers to them. These kinds of pointers that point to the arrays are called array pointers or pointers to arrays.

In this article, we will discuss what is a pointer to an array, how to create it and what are its applications in C++.

Before we start discussing pointers to arrays, let's first recall what are pointer and Arrays:

What are Arrays?

 An array is a collection of elements of similar data types that are stored in contiguous memory locations. The elements are of the same data type. The name of the array points to the memory address of its first element.

The syntax to declare an array in C++ is given below:

datatype array_name[sizeof_array];  

What are Pointers?

Pointers are variables that store the memory addresses of other variables or any data structures in C++.

The syntax to declare a pointer is given below:

data_type *pointer_name ;  

Pointers to an Array in C++

Pointers to an array is the pointer that points to the array. It is the pointer to the first element of the pointer instead of the whole array but we can access the whole array using pointer arithmetic.

Syntax to Declare Array Pointer

// for array of type: type arr_name[size];     type *ptr_name = &arr_name  

As you can see, this declaration is similar to the pointer to a variable. It is again because this pointer points to the first element of the array.

Note: The array name is also a pointer to its first element so we can also declare a pointer to the array as: type *ptr_name = arr_name;

Examples of Pointer to an Array in C++

Example 1: Program to Demonstrate That Array Name Is the Pointer to Its First Element

C++
// C++ program to demonstrate accessing array's first // element using array name and pointer #include <iostream> using namespace std;  int main() {     int arr[5] = { 1, 2, 3, 4, 5 };     int* ptr = arr;      // printing first element of an array using array name     // and dereference operator     cout << "*arr = " << *arr << endl;     cout << "arr = " << arr << endl;     cout << "*ptr = " << *ptr << endl;     cout << "ptr = " << ptr << endl;      return 0; } 

Output
*arr = 1  arr = 0x7ffc096e3bc0  *ptr = 1  ptr = 0x7ffc096e3bc0    

From the above code, we can see that the pointer "ptr" and "arr" point to the same memory address and to the same element.

Example 2: Program to Traverse Whole Array using Pointer

C++
// C++ program to demonstrate pointer to entire array  #include <iostream> using namespace std;  int main() {     int size = 5;     int arr[size] = { 1, 2, 3, 4, 5 };      // Pointer pointing to the entire array     int* ptr = arr;      // Loop through the array using pointer     for (int i = 0; i < size; ++i) {         // Accessing elements using pointer arithmetic         cout << "Value at index " << i << ": " << *(ptr + i)              << endl;     }      return 0; } 

Output
Value at index 0: 1  Value at index 1: 2  Value at index 2: 3  Value at index 3: 4  Value at index 4: 5    

Pointers to Multidimensional Arrays in C++

Pointers to multidimensional arrays are declared in a different way as compared to single-dimensional arrays. Here, we also need to mention the information about the dimensions and their size in the pointer declaration. Also, we need to take care of operator precedence of array subscript operator [] and dereference operator * as a little mistake can lead to the creation of whole different pointer.

Syntax to Declare Pointer to 2D Array

data_type (*array_name)[column_size];

For example,

int (*ptr)[4] = arr;

Here, int arr[3][4] declares an array with 3 rows (outer arrays) and 4 columns (inner arrays) of integers. If we declare an array as int arr[n][m] then n is the number of rows or we can say number of arrays it stores and m denotes the number of element each array(row) have in integers as int data type is declared.

Syntax to Declare Pointer to 3D Array

data_type (*array_name)[col_size][depth_size];

For example, for array int arr[1][2][3], we can delcare a pointer to it as:

int (*ptr)[2][3] = arr;

Examples of Pointer to Multidimensional Arrays

Example 1: Accessing Array Elements using Pointer.

C++
// C++ program to demonstrate accessing a 2D array using a // pointer  #include <iostream> using namespace std;  int main() {     // Initializing a 2D array     int arr[3][4] = { { 1, 2, 3, 4 },                       { 5, 6, 7, 8 },                       { 9, 10, 11, 12 } };      // Pointer to a 1D array of size 4     int(*ptr)[4];      // Assigning the base address of the 2D array to the     // pointer     ptr = arr;      // Loop to iterate through each element of the 2D array     for (int i = 0; i < 3; ++i) {         for (int j = 0; j < 4; ++j) {             cout << ptr[i][j] << " ";         }         cout << endl;     }      return 0; } 

Output
1 2 3 4   5 6 7 8   9 10 11 12   

Example 2: Program to Pass 3D Array to Functions

C++
// C++ program to demonstrate passing a 3D array to a // function  #include <iostream> using namespace std;  // Function to display the elements of a 3D array void display(int arr[][2][3], int rows, int cols, int depth) {     cout << "Displaying 3D array elements:" << endl;     for (int i = 0; i < rows; ++i) {         for (int j = 0; j < cols; ++j) {             for (int k = 0; k < depth; ++k) {                 cout << arr[i][j][k] << " ";             }             cout << endl;         }         cout << endl;     } }  int main() {     // Initializing a 3D array     int arr[2][2][3] = { { { 1, 2, 3 }, { 4, 5, 6 } },                          { { 7, 8, 9 }, { 10, 11, 12 } } };      // Dimensions of the 3D array     int rows = 2, cols = 2, depth = 3;      // Passing the 3D array to the function     display(arr, rows, cols, depth);      return 0; } 

Output
Displaying 3D array elements:  1 2 3   4 5 6     7 8 9   10 11 12     

Applications of Pointers to Arrays in C++

Following are some main applications of pointers to arrays in C++:

  • Passing Arrays to Functions: Pointers allow us to pass arrays to functions without copying the entire array. This can improve performance for large arrays.
  • Dynamic Memory Allocation: Pointers are used to allocate and deallocate memory dynamically at runtime using functions like new and delete. We can use pointers to arrays to create dynamic arrays.
  • Accessing Array Elements: Pointers can be used to access elements of an array in an efficient manner.

Next Article
Pointer to Arrays in Objective-C

A

amritkumasakv
Improve
Article Tags :
  • C++
  • Geeks Premier League
  • cpp-array
  • cpp-pointer
  • Geeks Premier League 2023
Practice Tags :
  • CPP

Similar Reads

  • Pointer vs Array in C
    Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being:   1. the sizeof operator sizeof(array) returns the amount of memory used by all elements in the array sizeof(pointer) only returns the amount of memory used by the pointer variable itself  2.
    1 min read
  • Pointers vs Array in C++
    Arrays and pointers are two derived data types in C++ that have a lot in common. In some cases, we can even use pointers in place of arrays. But even though they are so closely related, they are still different entities. In this article, we will study how the arrays and pointers are different from e
    3 min read
  • Pointer to an Array | Array Pointer
    A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements. Example: [GFGTABS] C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int
    5 min read
  • Is an Array Name a Pointer?
    In C, arrays and pointers are closely related and are often considered same by many people but this a common misconception. Array names are not a pointer. It is actually a label that refers to a contiguous block of memory where the elements are stored. In this article, we will learn more about the a
    4 min read
  • Pointer to Arrays in Objective-C
    In Objective-C, a pointer to an array is a way to store multiple values of the same data type in contiguous memory locations. These arrays can be manipulated and accessed using pointers, which are variables that store the memory address of another variable. In Objective-C, pointers to arrays are use
    4 min read
  • array::size() in C++ STL
    The array::size() method is used to find the number of elements in the array container. It is the member method std::array class defined inside <array> header file. In this article, we will learn about the array::size() method in C++. Example: [GFGTABS] C++ // C++ Program to illustrate the use
    2 min read
  • Arrays and Strings in C++
    Arrays An array in C or C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar types of elements as in the data type must be the same for all elements. They can be used to store the collection
    5 min read
  • Reference to Array in C++
    Reference to an array means aliasing an array while retaining its identity. Reference to an array will not be an int* but an int[]. Let us discuss this in detail by discussing the difference between these two. This is quite weird that int[] is the same as int* but still compiler perspective on both
    4 min read
  • void Pointer in C++
    In C++, a void pointer is a pointer that is declared using the 'void' keyword (void*). It is different from regular pointers it is used to point to data of no specified data type. It can point to any type of data so it is also called a "Generic Pointer". Syntax of Void Pointer in C++void* ptr_name;
    7 min read
  • array::operator[ ] in C++ STL
    Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences