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++
#include <iostream>
using namespace std;
int main()
{
int(*a)[5];
int b[5] = { 1, 2, 3, 4, 5 };
int i = 0;
a = &b;
for (i = 0; i < 5; i++)
cout << *(*a + i) << endl;
return 0;
}
C
#include <stdio.h>
int main()
{
int(*a)[5];
int b[5] = { 1, 2, 3, 4, 5 };
int i = 0;
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++
#include <iostream>
using namespace std;
const int SIZE = 3;
int main()
{
int arr[] = { 1, 2, 3 };
int i, *ptr[SIZE];
for (i = 0; i < SIZE; i++) {
ptr[i] = &arr;[i];
}
for (i = 0; i < SIZE; i++) {
cout << "Value of arr[" << i << "] = " << *ptr[i] << endl;
}
}
C
#include <stdio.h>
const int SIZE = 3;
void main()
{
int arr[] = { 1, 2, 3 };
int i, *ptr[SIZE];
for (i = 0; i < SIZE; i++) {
ptr[i] = &arr;[i];
}
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()
{
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
21 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...