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

Array of Pointers in C

Uploaded by

umathamizh0306
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)
35 views

Array of Pointers in C

Uploaded by

umathamizh0306
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/ 5

Array of Pointers in C

Last Updated : 30 Jul, 2024



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.
 // 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
Explanation:
Explanation:
As shown in the above example, each element of the array is a pointer
pointing to an integer. We can access the value of these integers by first
selecting the array element and then dereferencing it to get the value.
Array of Pointers to Character
One of the main applications of the array of pointers is to store multiple
strings as an array of pointers to characters. Here, each pointer in the array
is a character pointer that points to the first character of the string.
Syntax:
yntax:
char *array_name [array_size];
After that, we can assign a string of any length to these pointers.
Example:
C
char* arr[5]
= { "gfg", "geek", "Geek", "Geeks", "GeeksforGeeks" }
This method of storing strings has the advantage of the traditional array of
strings. Consider the following two examples:
Example 1:
C
// C Program to print Array of strings without array of pointers
#include <stdio.h>
int main()
{
char str[3][10] = { "Geek", "Geeks", "Geekfor" };

printf("String array Elements are:\n");

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


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

return 0;
}

Output
String array Elements are:
Geek
Geeks
Geekfor
In the above program, we have declared the 3 rows and 10 columns of our array of
strings. But because of predefining the size of the array of strings the space consumption
of the program increases if the memory is not utilized properly or left unused. Now let’s try
to store the same strings in an array of pointers.
// C Program to print Array of strings with array of pointers
#include <stdio.h>

int main() {
// Declare an array of pointers to characters
char* arr[] = { "geek", "Geeks", "Geeksfor" };

// Print each string and its address


for (int i = 0; i < 3; i++) {
printf("%s\n", arr[i]);
}

// Print addresses of each string


for (int i = 0; i < 3; i++) {
printf("Address of arr[%d]: %p\n", i, (void*)arr[i]);
}

return 0;
}
Output
geek
Geeks
Geeksfor
Address of arr[0]: 0x400634
Address of arr[1]: 0x400639
Address of arr[2]: 0x40063f
Here, the total memory used is the memory required for storing the strings and pointers
without leaving any empty space hence, saving a lot of wasted space. We can understand
this using the image shown below.
The space occupied by the array of pointers to characters is shown by solid green blocks
excluding the memory required for storing the pointer while the space occupied by the
array of strings includes both solid and light green blocks.
Array of Pointers to Different Types
Not only we can define the array of pointers for basic data types like int, char, float, etc.
but we can also define them for derived and user-defined data types such as arrays,
structures, etc. Let’s consider the below example where we create an array of pointers
pointing to a function for performing the different operations.
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