Pointer to an Array | Array Pointer
Last Updated :
10 Jan, 2025
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.
Consider the following example:
C
#include<stdio.h>
int main() {
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
printf("%p\n", ptr);
return 0;
}
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 Pointer to Array
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,
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 Pointer to an Array
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;
}
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;
}
But again, we have to hardcode the size information.
Pointer to the First Element of Array vs Pointer to Array
The pointer that points to the 0th element of array and the 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 *p1;
// Pointer to an array of 5 integers
int(*p2)[5];
// Points to 0th element of the arr
p1 = arr;
// Points to the whole array arr
p2 = &arr;
printf("p1 = %p\n", p1);
printf("*p2 = %p\n\n", *p2);
// incrementing both pointers
p1++;
p2++;
printf("p1 = %p\n", p1);
printf("*p2 = %p", p2);
return 0;
}
Outputp1 = 0x7ffea4a54ba0
*p2 = 0x7ffea4a54ba0
p1 = 0x7ffea4a54ba4
*p2 = 0x7ffea4a54bb4
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.

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.
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;
}
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.
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;
}
Output1 2
3 4
5 6
7 8
9 10
11 12
Similar Reads
C Pointers
A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it holds the address where the value is stored in memory. There are 2 important operators that we will use in pointers concepts i.e. Dereferencing operator(*) used to declare pointer variab
10 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 a
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
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. Consider the following example: [GFGTABS] C #include<stdio.h> int main() { int arr[5] =
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
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. the
1 min read
NULL Pointer in C
The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard: “An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resu
4 min read
Dangling, Void , Null and Wild Pointers in C
In C programming pointers are used to manipulate memory addresses, to store the address of some variable or memory location. But certain situations and characteristics related to pointers become challenging in terms of memory safety and program behavior these include Dangling (when pointing to deall
6 min read
Near, Far and Huge Pointers in C
In older times, the intel processors had 16-bit registers, but the address bus was 20-bits wide. Due to this, CPU registers were not able to hold the entire address at once. As a solution, the memory was divided into segments of 64 kB size, and the near pointers, far pointers, and huge pointers were
4 min read
restrict Keyword in C
The restrict keyword is a type qualifier that was introduced in the C99 standard. It is used to tell the compiler that a pointer is the only reference or access point to the memory it points to, allowing the compiler to make optimizations based on that information. Let's take a look at an example: [
3 min read