0% found this document useful (0 votes)
61 views2 pages

Array of Pointers in C

C PROGRAMMING NOTES

Uploaded by

maryjoseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Array of Pointers in C

C PROGRAMMING NOTES

Uploaded by

maryjoseph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Array of Pointers in C

Last Updated : 27 Feb, 2023



In C, a pointer array is a homogeneous collection of indexed pointer variables that are references
to a memory location. It is generally used in C Programming when we want to point at multiple
memory locations of a similar data type in our C program. We can access the data by
dereferencing the pointer pointing to it.
Syntax:
pointer_type *array_name [array_size];
Here,
 pointer_type: Type of data the pointer is pointing to.
 array_name: Name of the array of pointers.
 array_size: Size of the array of pointers.
Note: It is important to keep in mind the operator precedence and associativity in the array of
pointers declarations of different type as a single change will mean the whole different thing.
For example, enclosing *array_name in the parenthesis will mean that array_name is a
pointer to an array.

// C program to demonstrate the use of array of pointers


#include <stdio.h>

int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;

// array of pointers to integers


int* ptr_arr[3] = { &var1, &var2, &var3 };

// traversing using loop


for (int i = 0; i < 3; i++) {
printf("Value of var%d: %d\tAddress: %p\n", i + 1, *ptr_arr[i], ptr_arr[i]);
}

return 0;
}

Output
Value of var1: 10 Address: 0x7fff1ac82484
Value of var2: 20 Address: 0x7fff1ac82488
Value of var3: 30 Address: 0x7fff1ac8248c

// C program to illustrate the use of array of pointers to


// characters
#include <stdio.h>

int main()
{

char* arr[3] = { "geek", "Geeks", "Geeksfor" };

for (int i = 0; i < 3; i++) {


printf("%s\n", arr[i]);
}

return 0;
}
Output
geek
Geeks
Geeksfor
Application of Array of Pointers
An array of pointers is useful in a wide range of cases. Some of these applications are listed
below:
 It is most commonly used to store multiple strings.
 It is also used to implement LinkedHashMap in C and also in the Chaining technique of
collision resolving in Hashing.
 It is used in sorting algorithms like bucket sort.
 It can be used with any pointer type so it is useful when we have separate declarations of
multiple entities and we want to store them in a single place.
Disadvantages of Array of Pointers
The array of pointers also has its fair share of disadvantages and should be used when the
advantages outweigh the disadvantages. Some of the disadvantages of the array of pointers are:
 Higher Memory Consumption: An array of pointers requires more memory as compared
to plain arrays because of the additional space required to store pointers.
 Complexity: An array of pointers might be complex to use as compared to a simple array.
 Prone to Bugs: As we use pointers, all the bugs associated with pointers come with it so we
need to handle them carefully.

You might also like