0% found this document useful (0 votes)
76 views

C Array of Pointers

An array of pointers allows storing pointers to different data types in an array. The document explains how to declare and use an array of pointers to integers. It demonstrates initializing an array of pointers to store the addresses of integers in an array. Similarly, it shows how to use an array of pointers to strings to store a list of names. Accessing elements of the pointer array through the pointers accesses the underlying integer or string values. Arrays of pointers provide flexibility to work with heterogeneous data types stored in an array.

Uploaded by

m.kanzit.c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

C Array of Pointers

An array of pointers allows storing pointers to different data types in an array. The document explains how to declare and use an array of pointers to integers. It demonstrates initializing an array of pointers to store the addresses of integers in an array. Similarly, it shows how to use an array of pointers to strings to store a list of names. Accessing elements of the pointer array through the pointers accesses the underlying integer or string values. Arrays of pointers provide flexibility to work with heterogeneous data types stored in an array.

Uploaded by

m.kanzit.c
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C - ARRAY OF POINTERS

http://www.tuto rialspo int.co m/cpro g ramming /c_array_o f_po inte rs.htm

Co pyrig ht tuto rials po int.co m

Before we understand the concept of arrays of pointers, let us consider the following example, which makes use
of an array of 3 integ ers:
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i;
for (i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, var[i] );
}
return 0;
}

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

T here may be a situation when we want to maintain an array, which can store pointers to an int or char or any other
data type available. Following is the declaration of an array of pointers to an integ er:
int *ptr[MAX];

T his declares ptr as an array of MAX integ er pointers. T hus, each element in ptr, now holds a pointer to an int
value. Following example makes use of three integ ers, which will be stored in an array of pointers as follows:
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++)
{
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++)
{
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}

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

You can also use an array of pointers to character to store a list of string s as follows:

#include <stdio.h>
const int MAX = 4;
int main ()
{
char *names[] = {
"Zara
"Hina
"Nuha
"Sara
};
int i = 0;

Ali",
Ali",
Ali",
Ali",

for ( i = 0; i < MAX; i++)


{
printf("Value of names[%d] = %s\n", i, names[i] );
}
return 0;
}

When the above code is compiled and executed, it produces the following result:
Value
Value
Value
Value

of
of
of
of

names[0]
names[1]
names[2]
names[3]

=
=
=
=

Zara
Hina
Nuha
Sara

Ali
Ali
Ali
Ali

You might also like