Difference between pointer to an array and array of pointers
Last Updated :
21 Feb, 2023
Pointer to an array: Pointer to an array is also known as array pointer. We are using the pointer to access the components of the array.
int a[3] = {3, 4, 5 };
int *ptr = a;
We have a pointer ptr that focuses to the 0th component of the array. We can likewise declare a pointer that can point to whole array rather than just a single component of the array. Syntax:
data type (*var name)[size of array];
Declaration of the pointer to an array:
// pointer to an array of five numbers
int (* ptr)[5] = NULL;
The above declaration is the pointer to an array of five integers. We use parenthesis to pronounce pointer to an array. Since subscript has higher priority than indirection, it is crucial to encase the indirection operator and pointer name inside brackets. Example:
C++
// C++ program to demonstrate
// pointer to an array.
#include <iostream>
using namespace std;
int main()
{
// Pointer to an array of five numbers
int(*a)[5];
int b[5] = { 1, 2, 3, 4, 5 };
int i = 0;
// Points to the whole array b
a = &b;
for (i = 0; i < 5; i++)
cout << *(*a + i) << endl;
return 0;
}
// This code is contributed by sarajadhav12052009
C
// C program to demonstrate
// pointer to an array.
#include <stdio.h>
int main()
{
// Pointer to an array of five numbers
int(*a)[5];
int b[5] = { 1, 2, 3, 4, 5 };
int i = 0;
// Points to the whole array b
a = &b;
for (i = 0; i < 5; i++)
printf("%d\n", *(*a + i));
return 0;
}
Array of pointers: "Array of pointers" is an array of the pointer variables. It is also known as pointer arrays. Syntax:
int *var_name[array_size];
Declaration of an array of pointers:
int *ptr[3];
We can make separate pointer variables which can point to the different values or we can make one integer array of pointers that can point to all the values. Example:
C++
// C++ program to demonstrate
// example of array of pointers.
#include <iostream>
using namespace std;
const int SIZE = 3;
int main()
{
// creating an array
int arr[] = { 1, 2, 3 };
// we can make an integer pointer array to
// storing the address of array elements
int i, *ptr[SIZE];
for (i = 0; i < SIZE; i++) {
// assigning the address of integer.
ptr[i] = &arr[i];
}
// printing values using pointer
for (i = 0; i < SIZE; i++) {
cout << "Value of arr[" << i << "] = " << *ptr[i] << endl;
}
}
// This code is contributed by sarajadhav12052009
C
// C program to demonstrate
// example of array of pointers.
#include <stdio.h>
const int SIZE = 3;
void main()
{
// creating an array
int arr[] = { 1, 2, 3 };
// we can make an integer pointer array to
// storing the address of array elements
int i, *ptr[SIZE];
for (i = 0; i < SIZE; i++) {
// assigning the address of integer.
ptr[i] = &arr[i];
}
// printing values using pointer
for (i = 0; i < SIZE; i++) {
printf("Value of arr[%d] = %d\n", i, *ptr[i]);
}
}
Output:Value of arr[0] = 1
Value of arr[1] = 2
Value of arr[2] = 3
Example: We can likewise make an array of pointers to the character to store a list of strings.
C
#include <stdio.h>
const int size = 4;
void main()
{
// array of pointers to a character
// to store a list of strings
char* names[] = {
"amit",
"amar",
"ankit",
"akhil"
};
int i = 0;
for (i = 0; i < size; i++) {
printf("%s\n", names[i]);
}
}
Output:amit
amar
ankit
akhil
Similar Reads
Whatâs difference between âarrayâ and â&arrayâ for âint array[5]â ? If someone has defined an array such as âint array[5]â, whatâs the meaning of âarrayâ or â&arrayâ? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out! To
4 min read
Difference between Arrays and Pointers The array and pointers are derived data types that have lots of differences and similarities. In some cases, we can even use pointers in place of an array, and arrays automatically get converted to pointers when passed to a function. So, it is necessary to know about the differences between arrays a
7 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 Structure and Array in C Array in C An array is collection of items stored at contiguous memory locations. Structure in C A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type. Difference between Structure and Array AR
2 min read
Difference between int *a and int **a in C In C, the declarations int *a and int **a represent two different concepts related to pointers. Pointers play a fundamental role in memory management and data manipulation in C programming so it is important to have a clear understanding of them. What does int * means? This declares a pointer to an
3 min read
Difference between Iterators and Pointers in C++ with Examples In C++ programming, we have both pointers and iterators that are used in managing and manipulating data structures. There are many similarities between iterators and pointers in their ability to reference and dereference memory, but there are certain differences between the two. Understanding the di
4 min read