- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Array of Pointers
![]() Share with a Friend |
C Programming - C Array of Pointers
C Array of Pointers
An array of pointers is an array where each element is a pointer that can store the address of another variable. It allows you to create arrays of strings, arrays of dynamically allocated memory, or even arrays of structures where each pointer can point to a different memory location.
Understanding Array of Pointers
- In an array of pointers, each element is a pointer that stores the address of an element in memory, and the type of the array is determined by the pointer's type.
- For example, an array of int pointers (int *arr[5]) means each element of the array is a pointer to an int.
Syntax for Array of Pointers:
C
type *array_name[size];
- type: Data type of the elements that the pointers in the array will point to.
- array_name: Name of the array.
- size: Number of elements in the array.
Example of Array of Pointers:
- Array of Pointers to Integers
C
#include <stdio.h>
int main() {
int num1 = 10, num2 = 20, num3 = 30;
// Array of pointers to integers
int *arr[3];
// Assigning addresses of variables to the array elements
arr[0] = &num1;
arr[1] = &num2;
arr[2] = &num3;
// Accessing values using array of pointers
printf("Value of num1: %d\n", *arr[0]); // 10
printf("Value of num2: %d\n", *arr[1]); // 20
printf("Value of num3: %d\n", *arr[2]); // 30
return 0;
}
Output:
Value of num1: 10
Value of num2: 20
Value of num3: 30
- Array of Pointers to Strings (Array of String Literals)
A common use of an array of pointers is when dealing with arrays of strings (which are actually arrays of character pointers).
C
#include <stdio.h>
int main() {
// Array of pointers to string literals
char *arr[] = {"Hello", "World", "Array", "of", "Pointers"};
// Accessing and printing each string
for (int i = 0; i < 5; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Output:
Hello
World
Array
of
Pointers
- Array of Pointers to Arrays (2D Array)
An array of pointers can also be used to represent a 2D array, where each element of the array points to another array.
C
#include <stdio.h>
int main() {
// Array of 3 pointers to arrays of 2 integers
int *arr[3];
// Initializing arrays of integers
int arr1[] = {1, 2};
int arr2[] = {3, 4};
int arr3[] = {5, 6};
// Assigning the addresses of arrays to the array of pointers
arr[0] = arr1;
arr[1] = arr2;
arr[2] = arr3;
// Accessing elements through array of pointers
printf("arr[0]: %d, %d\n", arr[0][0], arr[0][1]); // 1, 2
printf("arr[1]: %d, %d\n", arr[1][0], arr[1][1]); // 3, 4
printf("arr[2]: %d, %d\n", arr[2][0], arr[2][1]); // 5, 6
return 0;
}
Output:
arr[0]: 1, 2
arr[1]: 3, 4
arr[2]: 5, 6
Advantages of Array of Pointers:
- Memory Efficiency:
- It allows you to dynamically allocate memory for individual elements, which is useful in scenarios where the size of the elements may vary.
- Flexibility:
- Each element in the array can point to different memory locations, making it flexible for handling dynamic data structures like linked lists, trees, etc.
- Efficient for Strings:
- It is often used for handling strings in C because each element of the array can point to a different string (which is a character array).
Limitations of Array of Pointers:
- Pointer Arithmetic:
- You need to be careful with pointer arithmetic, especially when accessing array elements. Incorrect pointer manipulation can lead to undefined behavior.
- Complexity:
- Understanding and maintaining pointer-based structures can become complex for large programs, especially when combined with dynamic memory allocation.
Conclusion:
An array of pointers is a powerful and versatile construct in C programming. It allows you to store memory addresses of various data types, providing flexibility in data management, particularly when working with dynamically allocated memory or strings. Proper understanding and careful manipulation of array of pointers can enhance the performance and efficiency of your programs.
