Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Difference between constant pointer, pointers to constant, and constant pointers to constants
Next article icon

Pointer to an Array | Array Pointer

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

C
#include<stdio.h>  int main() {   int arr[5] = { 1, 2, 3, 4, 5 };   int *ptr = arr;    printf("%p\n", ptr);   return 0; } 

Output
0x7ffde273ac20 

In the above program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can point to whole array instead of only one element of the array. This pointer is useful when talking about multidimensional arrays. 

Syntax of Array Pointer

An array pointer can be declared as shown:

C++
type(*ptr)[size]; 

where,

  • type: Type of data that the array holds.
  • ptr: Name of the pointer variable.
  • size: Size of the array to which the pointer will point.

For example,

C++
int (*ptr)[10]; 

Here ptr is pointer that points to an array of 10 integers. Since subscript have higher precedence than indirection, it is necessary to enclose the indirection operator and pointer name inside parentheses.

Examples of Pointer to Array

The following examples demonstrate the use pf pointer to an array in C and also highlights the difference between the pointer to an array and pointer to the first element of an array.

Access Array Using Array Pointer

In the below code, the type of ptr is pointer to an array of 10 integers.

C
#include <stdio.h>  int main() {     int arr[3] = { 5, 10, 15 };	   	int n = sizeof(arr) / sizeof(arr[0]);      // Declare pointer variable     int (*ptr)[3];      // Assign address of val[0] to ptr.     // We can use ptr=&val[0];(both are same)     ptr = &arr; 	   	for (int i = 0; i < n; i++)     	printf("%d ", (*ptr)[i]);      return 0; } 

Output
5 10 15 

Find the Size of Array Passed to a Function

Normally, it is impossible to find the size of array inside a function, but if we pass the pointer to an array, the it is possible.

C
#include <stdio.h>  // Function that takes array as argument void foo(int (*arr)[5]) {     printf("%lu ", sizeof(*arr)); }  int main() {     int arr[5];      // Passing the address of arr     foo(&arr);      return 0; } 

Output
20 

But again, we have to hardcode the size information.

Pointer to the First Element of Array vs Array Pointer

The pointer that points to the 0th element of array and the array pointer that points to the whole array are totally different. The following program shows this: 

C
#include <stdio.h>  int main() {     int arr[5] = {1, 2, 3, 4, 5};      // Create a pointer to integer     int *p;      // Pointer to an array of 5 integers     int(*ptr)[5];      // Points to 0th element of the arr     p = arr;      // Points to the whole array arr     ptr = &arr;      	printf("p = %p\n", p);   	printf("*ptr = %p\n\n", *ptr);      	// incrementing both pointers     p++;     ptr++;     printf("p = %p\n", *p);   	printf("*ptr = %p", ptr);      return 0; } 

Output
p = 0x7fff81e4caf0 *ptr = 0x7fff81e4caf0  p = 0x7fff81e4caf4 *ptr = 0x7fff81e4cb04

Here, p is pointer to 0th element of the array arr, while ptr is a pointer that points to the whole array arr. 

  • The base type of p is int while base type of ptr is 'an array of 5 integers'.
  • We know that the pointer arithmetic is performed relative to the base size, so if we write ptr++, then the pointer ptr will be shifted forward by 20 bytes.

The following figure shows the pointer p and ptr. The darker arrow denotes a pointer to an array.  (address may vary in each execution of the program)

On dereferencing a pointer expression, we get a value pointed to by that pointer expression. The pointer to an array point to an array, so on dereferencing it, we should get the array, and the name of the array denotes the base address. So, whenever a pointer to an array is dereferenced, we get the base address of the array to which it points.

Pointer to Multidimensional Arrays

The concept of pointer to an array can be extended to multidimensional arrays too:

1. Pointers to 2D Arrays

To define a pointer to a 2D array, both the number of rows and columns of the array must be specified in the pointer declaration.

C++
type *(ptr)[row][cols] 

Let's take a look at an example:

C++
#include <stdio.h>  int main() {     int arr[2][3] = {{1, 2, 3},                     {4, 5, 6}};      	// pointer to above array   	int (*ptr)[2][3] = &arr;    	// Traversing the arry using ptr     for (int i = 0; i < 2; i++) {         for (int j = 0; j < 3; j++) {                 printf("%d ", (*ptr)[i][j]);         }         printf("\n");     }      return 0; } 

Output
1 2 3  4 5 6  

2. Pointers to 3D Arrays

To define a pointer to a 2D array, both the number of rows and columns of the array must be specified in the pointer declaration.

C++
type *(ptr)[depth][row][cols] 

Let's take a look at an example:

C
#include <stdio.h>  int main() {     int arr[2][3][2] = {{{1, 2}, {3, 4}, {5, 6}},                        {{7, 8}, {9, 10}, {11, 12}}};      // Pointer to the 3D array     int (*ptr)[2][3][2] = &arr;      // Traversing the 3D array using the pointer     for (int i = 0; i < 2; i++) {         for (int j = 0; j < 3; j++) {             for (int k = 0; k < 2; k++) {                 printf("%d ", (*ptr)[i][j][k]);             }             printf("\n");         }         printf("\n");     }      return 0; } 

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

Next Article
Difference between constant pointer, pointers to constant, and constant pointers to constants

A

Anuj Chauhan
Improve
Article Tags :
  • Misc
  • C Language
  • C++
  • cpp-pointer
  • C-Pointers
Practice Tags :
  • CPP
  • Misc

Similar Reads

    C Pointers
    A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
    9 min read
    Pointer Arithmetics in C with Examples
    Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
    10 min read
    Applications of Pointers in C
    Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
    4 min read
    Passing Pointers to Functions in C
    Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
    2 min read
    C - Pointer to Pointer (Double Pointer)
    In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer.Let's take a look at
    5 min read
    Chain of Pointers in C with Examples
    Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
    5 min read
    Function Pointer in C
    In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
    6 min read
    How to Declare a Pointer to a Function?
    A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w
    2 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:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
    5 min read
    Difference between constant pointer, pointers to constant, and constant pointers to constants
    In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
    3 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