Array of Pointers in C
Array of Pointers in C
Array of Pointers in C
The name of an array can be used as a pointer because it holds the address to the
first element of the array. If we store the address of an array in another pointer, then
it is possible to manipulate the array using pointer arithmetic.
The following example demonstrates how you can create and use an array of
pointers. Here, we are declaring three integer variables and to access and use them,
we are creating an array of pointers. With the help of an array of pointers, we are
printing the values of the variables.
#include <stdio.h>
int main() {
// Declaring integers
int var1 = 1;
int var2 = 2;
int var3 = 3;
https://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm 1/5
6/16/24, 1:04 PM Array of Pointers in C
// Accessing values
for (int i = 0; i < 3; i++) {
printf("Value at ptr[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
Output
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
There may be a situation when we want to maintain an array that can store pointers
to an "int" or "char" or any other data type available.
int *ptr[MAX];
It declares ptr as an array of MAX integer pointers. Thus, each element in ptr holds
a pointer to an int value.
Example
The following example uses three integers, which are stored in an array of pointers,
as follows −
https://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm 2/5
6/16/24, 1:04 PM Array of Pointers in C
#include <stdio.h>
int main(){
return 0;
}
Output
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
#include <stdio.h>
int main(){
char *names[] = {
"Zara Ali",
https://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm 3/5
6/16/24, 1:04 PM Array of Pointers in C
"Hina Ali",
"Nuha Ali",
"Sara Ali"
};
int i = 0;
return 0;
}
Output
When the above code is compiled and executed, it produces the following result −
Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Declaring a structure
typedef struct {
char title[50];
float price;
} Book;
https://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm 4/5
6/16/24, 1:04 PM Array of Pointers in C
return 0;
}
Output
When the above code is compiled and executed, it produces the following result −
https://www.tutorialspoint.com/cprogramming/c_array_of_pointers.htm 5/5