Last Updated : 30 Apr, 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.
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 Array PointerAn array pointer can be declared as shown:
C++
where,
For example,
C++
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 ArrayThe 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 PointerIn 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 Array PointerThe 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;
}
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 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 ArraysThe 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++
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.
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;
}
1 2 3 4 5 6 7 8 9 10 11 12
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4