For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
In C programming, debugging and memory management often involve complex concepts like pointers. When combined with arrays, pointers offer a unique level of flexibility and control. This brings us to the Array of Pointers in C, a powerful structure that enables efficient handling of data. By using an array of pointers, developers can manipulate arrays more dynamically. This concept can be especially useful when working with multidimensional arrays or handling different data types. Visit our Software Engineering courses to get hands-on experience
While the assignment operator in C is often used in pointer operations, understanding how pointers interact with arrays is crucial for efficient memory management. Before diving deeper, it's important to understand the relationship between arrays and pointers and how arrays of pointers offer unique advantages and challenges.
An array of pointers in C is a collection of pointers, each pointing to a different variable or memory location. Unlike a regular array where elements are of the same data type, an array of pointers allows each element to point to different types of data. This makes it versatile and useful in many advanced programming scenarios, such as handling strings or multidimensional arrays.
Supercharge your software engineering career with these top courses:
In C, arrays and pointers are closely related. While an array holds a sequence of elements, a pointer holds the address of a variable or array element. Pointers and arrays are often interchangeable in C, allowing for efficient data manipulation and memory access. Understanding this relationship is key to mastering arrays of pointers. If you're new to pointers, check out our beginner's guide on Pointers in C
When working with an Array of Pointers in C, the declaration style differs from regular arrays. Instead of storing actual values, each element stores a memory address pointing to a value of a specific type.
Here’s how you can declare one.
int *arr[10];
This creates an array named arr with 10 elements. Each element is a pointer to an integer. So, arr[0] to arr[9] can hold the addresses of integer variables.
You often use this form when handling dynamic arrays, arrays of strings, or indirect memory access.
The general syntax is:
<type> *<array_name>[<size>];
This layout lets you manage multiple references efficiently without allocating actual values in the array.
Let’s see a simple example:
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
int *arr[3];
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
for (int i = 0; i < 3; i++) {
printf("Value at arr[%d] = %d\n", i, *arr[i]);
}
return 0;
}
Output:
Value at arr[0] = 10
Value at arr[1] = 20
Value at arr[2] = 30
Explanation:
This example highlights how flexible and powerful pointer arrays can be, especially when you want to manage multiple variables dynamically. To understand arrays better, especially one-dimensional ones, read One Dimensional Array in C.
An array of pointers in C offers significant flexibility and efficiency but also introduces some complexities. It's essential to weigh these pros and cons to determine when it's best to use this feature in your C programs.
Here Are Some Advantages of Using an Array of Pointers in C
Here Are Some Disadvantages of Using an Array of Pointers in C
For a quick refresher on basic arrays, visit Array in C
An Array of Pointers in C plays a vital role in real-world programs. Developers often use it in scenarios that demand flexible data handling.
These use cases highlight how arrays of pointers boost performance, modularity, and memory efficiency in real-time systems.
Explore Structure in C for a deeper understanding.
What Is an Array of Pointers to Characters in C?
In C programming, strings are arrays of characters. But when you manage multiple strings, using an Array of Pointers to Characters in C offers better memory usage and flexibility.
Unlike a 2D character array, this pointer-based array does not allocate memory for fixed-length strings. Instead, it stores addresses of string literals or dynamically allocated strings.
Example: Array of Pointers to String Literals
#include <stdio.h>
int main() {
const char *names[] = {"Ram", "Vicky", "Praveen", "Adarsh"};
for (int i = 0; i < 4; i++) {
printf("names[%d] = %s\n", i, names[i]);
}
return 0;
}
Output:
names[0] = Ram
names[1] = Vicky
names[2] = Praveen
names[3] = Adarsh
Explanation:
This approach is efficient because it doesn't reserve space for unused characters like in 2D arrays.
Example: Array of Pointers to Modifiable Strings
#include <stdio.h>
int main() {
char str1[] = "Hi";
char str2[] = "Hello";
char str3[] = "Welcome";
char *messages[] = {str1, str2, str3};
messages[1][0] = 'Y'; // Modify "Hello" to "Yello"
for (int i = 0; i < 3; i++) {
printf("messages[%d] = %s\n", i, messages[i]);
}
return 0;
}
Output:
messages[0] = Hi
messages[1] = Yello
messages[2] = Welcome
Explanation:
This flexibility is not available when pointing to string literals, as they are stored in read-only memory.
In an array of strings, each element points to the starting address of a string. The array itself is a collection of pointers, where each pointer points to a null-terminated string. This allows for flexible string management in C, where strings can be manipulated efficiently by updating their memory addresses. If you are dealing with an array of pointers to strings, check out useful String Functions in C.
Yes, you can create an array of pointers to different data types in C using void pointers. A void* can store the address of any data type, making it flexible for heterogeneous pointer arrays.
Example: Array of void* Pointers
#include <stdio.h>
int main() {
int a = 100;
float b = 12.5;
char c = 'X';
void *arr[3];
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
printf("Integer: %d\n", *(int*)arr[0]);
printf("Float: %.2f\n", *(float*)arr[1]);
printf("Char: %c\n", *(char*)arr[2]);
return 0;
}
Output:
Integer: 100
Float: 12.50
Char: X
Explanation:
This program stores addresses of an integer, float, and character in a void* array. Type casting is used while accessing each element to interpret the memory correctly.
A pointer to an array in C refers to a pointer that stores the address of an entire array, not just a single element. This is helpful when you want to manipulate all elements collectively or pass the array to functions.
Example: Pointer to an Array of Integers
#include <stdio.h>
int main() {
int nums[5] = {10, 20, 30, 40, 50};
int (*ptr)[5] = &nums;
for (int i = 0; i < 5; i++) {
printf("%d ", (*ptr)[i]);
}
return 0;
}
Output:
10 20 30 40 50
Explanation:
Here, ptr points to the entire array nums. The dereferencing (*ptr)[i] gives access to the elements.
Pointers can efficiently handle multidimensional arrays by accessing rows as pointers. This provides dynamic memory control and better abstraction for nested data.
Example: Using Pointers with 2D Arrays
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr[3];
for (int i = 0; i < 3; i++) {
arr[i] = (int*)malloc(2 * sizeof(int));
for (int j = 0; j < 2; j++) {
arr[i][j] = (i + 1) * (j + 1);
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
Output:
1 2
2 4
3 6
Explanation:
This is an array of 3 pointers, each pointing to a dynamically allocated array of 2 integers. It behaves like a 3x2 matrix.
A 3D array can be managed using pointers to 2D arrays. Pointer arithmetic enables traversal and manipulation of multi-level data like volumetric matrices.
Example: Using Pointers with 3D Array
#include <stdio.h>
int main() {
int arr[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
int (*ptr)[2][2] = arr;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("%d ", ptr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
return 0;
}
Output:
1 2
3 4
5 6
7 8
Explanation:
Here, ptr is a pointer to a 2D array of size [2][2]. It accesses the 3D array’s layers using standard indexing.Must Read: Dynamic Array in C
Below are a couple of examples showcasing how arrays of pointers are used in C:
#include <stdio.h>
int main() {
char *arr[] = {"Hello", "World", "Array", "Pointers"};
for (int i = 0; i < 4; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Output:
Hello
World
Array
Pointers
Explanation:
Each element in the array arr holds a pointer to a string literal. The loop prints each string.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr[5];
for (int i = 0; i < 5; i++) {
arr[i] = (int*)malloc(sizeof(int));
*(arr[i]) = i * 10;
}
for (int i = 0; i < 5; i++) {
printf("%d ", *(arr[i]));
}
return 0;
}
Output:
0 10 20 30 40
Explanation:
Each pointer in arr is dynamically allocated and initialized with a value. The loop prints the values by dereferencing each pointer.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr[3]; // Array of 3 int pointers
int sizes[] = {2, 4, 3}; // Sizes of each row
// Dynamic allocation for each row
for (int i = 0; i < 3; i++) {
arr[i] = (int*)malloc(sizes[i] * sizeof(int));
for (int j = 0; j < sizes[i]; j++) {
arr[i][j] = (i + 1) * (j + 1); // Initialize elements
}
}
// Print each row with its elements
for (int i = 0; i < 3; i++) {
printf("Row %d: ", i);
for (int j = 0; j < sizes[i]; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
// Free allocated memory
for (int i = 0; i < 3; i++) {
free(arr[i]);
}
return 0;
}
Output:
Row 0: 1 2
Row 1: 1 2 3 4
Row 2: 1 2 3
Explanation:
The array of pointers in C is a versatile and powerful feature, allowing for more dynamic and efficient memory management. Whether working with strings, multidimensional arrays, or diverse data types, understanding how to declare, use, and manage arrays of pointers is essential for C programmers. Mastery of this concept will lead to better memory control and more efficient program design. If you want to dive even deeper, explore the concept of Pointer to Pointer in C
Q1. What is an Array of Pointers in C?
An Array of Pointers in C is a collection where each element holds an address of a variable, not a value directly. It allows easy management of multiple variables or dynamic memory in a structured, flexible way.
Q2. How do you declare an Array of Pointers in C?
You can declare an array of pointers using syntax like int *arr[5];, where arr is an array of five pointers to integers. Each element points to an integer variable’s memory address.
Q3. What are the advantages of using an Array of Pointers in C?
Arrays of pointers offer efficient memory usage, faster access to data, and dynamic management of strings or structures. They are ideal when handling variable-length data or optimizing large data structures for speed.
Q4. What are the disadvantages of using an Array of Pointers in C?
Managing arrays of pointers requires careful memory handling. Risks include dangling pointers, memory leaks, and increased complexity, especially for beginners learning C programming. Proper initialization and deallocation are critical.
Q5. How is memory managed in an Array of Pointers to Strings in C?
Each string in an array of pointers occupies separate memory blocks. Only the starting addresses are stored in the array, saving memory but requiring attention to memory wastage and string termination with \0.
Q6. What is the difference between a Pointer to an Array and an Array of Pointers in C?
A Pointer to an Array points to an entire array, while an Array of Pointers holds addresses of individual elements or blocks. They serve different purposes and have distinct syntactical and memory behaviors.
Q7. Can we store function addresses in an Array of Pointers in C?
Yes, function pointers can be stored in an array. This technique allows you to call different functions dynamically, making your C programs modular, flexible, and highly optimized for specific tasks.
Q8. How are multidimensional arrays handled using pointers in C?
Multidimensional arrays can be accessed using a single pointer. By treating a 2D or 3D array as a continuous memory block, you can use pointer arithmetic to move across rows, columns, or layers.
Q9. Can an Array of Pointers in C point to different data types?
Normally, an array should contain pointers to the same data type. However, using void * pointers, you can store addresses of different types. This needs typecasting while accessing the stored values.
Q10. How does pointer arithmetic work in an Array of Pointers in C?
Pointer arithmetic allows you to move through memory addresses in an array. For example, incrementing a pointer in an array of integers moves to the next integer based on the size of the data type.
Q11. How does an Array of Pointers differ from a 2D array in C?
A 2D array is a fixed-size contiguous memory block, while an Array of Pointers can point to different sized blocks, saving memory. It offers flexibility but requires manual management of each element's memory.
Q12. Can we dynamically allocate memory for an Array of Pointers in C?
Yes, you can dynamically allocate memory for each pointer using malloc() or calloc(). This allows flexible memory management where you allocate only what is needed, avoiding wastage common in static arrays.
Q13. Why is null termination important in an Array of Character Pointers in C?
Null termination '\0' marks the end of a string. In arrays of character pointers, it ensures correct string processing during traversal, copying, or printing, preventing unexpected behaviors or memory errors.
Q14. What happens if we dereference an uninitialized pointer in an Array of Pointers?
Dereferencing an uninitialized pointer can cause undefined behavior, including program crashes. Always initialize pointers either by assigning them valid addresses or by setting them to NULL until they are properly assigned.
Q15. Are Arrays of Pointers faster than normal arrays in C?
In many scenarios, yes. Arrays of pointers avoid unnecessary data copying, especially with large or variable-sized data. Access times are faster because you deal directly with memory addresses, not entire data blocks.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.