- 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 Initialization of Pointer Arrays
![]() Share with a Friend |
C Programming - C Initialization of Pointer Arrays
C Initialization of Pointer Arrays
In C, pointer arrays are arrays where each element of the array is a pointer to a certain data type. Initializing pointer arrays is an important aspect of pointer manipulation. Below are various ways to initialize pointer arrays in C.
- Array of Pointers (Pointer Array)
An array of pointers is essentially an array where each element is a pointer to a variable of a particular type (for example, pointers to integers, characters, etc.).
Syntax:
C
data_type *array_name[size];
Here:
- data_type: Type of data the pointer will point to (e.g., int, char, float).
- array_name: The name of the pointer array.
- size: Number of elements in the pointer array.
- Example: Array of Pointers to Integers
Here’s an example of how you can declare and initialize an array of integer pointers:
C
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30; // Declare some integer variables
int *arr[3]; // Declare an array of 3 integer pointers
// Initialize the pointer array
arr[0] = &a; // arr[0] points to 'a'
arr[1] = &b; // arr[1] points to 'b'
arr[2] = &c; // arr[2] points to 'c'
// Access the values using the pointer array
printf("Value at arr[0]: %d\n", *arr[0]);
printf("Value at arr[1]: %d\n", *arr[1]);
printf("Value at arr[2]: %d\n", *arr[2]);
return 0;
}
Explanation:
- The array arr contains 3 elements, each of which is a pointer to an integer.
- Each element is initialized to point to a specific integer variable (a, b, or c).
- When dereferencing arr[0], arr[1], and arr[2], we access the values of a, b, and c, respectively.
Output:
Value at arr[0]: 10
Value at arr[1]: 20
Value at arr[2]: 30
- Pointer to Array Initialization
You can also initialize pointers to arrays, where a pointer points to the whole array.
Example: Pointer to an Array of Integers
C
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30}; // Declare an array
int *ptr = arr; // Pointer pointing to the array 'arr'
// Accessing elements using the pointer
printf("First element: %d\n", *ptr);
printf("Second element: %d\n", *(ptr + 1));
printf("Third element: %d\n", *(ptr + 2));
return 0;
}
Explanation:
- ptr points to the beginning of the array arr.
- Dereferencing ptr gives the first element of the array.
- By using pointer arithmetic (ptr + 1, ptr + 2), we can access the next elements of the array.
Output:
First element: 10
Second element: 20
Third element: 30
- Array of Pointers to Strings (Array of Character Pointers)
An array of pointers can also be used to store strings (arrays of characters). This is commonly done when working with multiple strings in C.
Example: Array of Strings
C
#include <stdio.h>
int main() {
// Array of pointers to strings
char *fruits[] = {"Apple", "Banana", "Cherry", "Date"};
// Print all the strings using the pointer array
for (int i = 0; i < 4; i++) {
printf("%s\n", fruits[i]);
}
return 0;
}
Explanation:
- fruits is an array of 4 pointers, each pointing to a string (a character array).
- Each pointer in the fruits array holds the address of a string literal.
Output:
Apple
Banana
Cherry
Date
- Initialization at the Time of Declaration
You can also initialize the pointer array at the time of declaration.
Example: Initialize Pointer Array at Declaration
C
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 30;
// Initialize the pointer array at the time of declaration
int *arr[] = {&a, &b, &c}; // Pointer array initialized with addresses of 'a', 'b', and 'c'
// Accessing values through the pointer array
printf("Value at arr[0]: %d\n", *arr[0]);
printf("Value at arr[1]: %d\n", *arr[1]);
printf("Value at arr[2]: %d\n", *arr[2]);
return 0;
}
Explanation:
- arr[] is initialized with the addresses of a, b, and c.
- Dereferencing arr[0], arr[1], and arr[2] will give the values of a, b, and c.
Output:
Value at arr[0]: 10
Value at arr[1]: 20
Value at arr[2]: 30
- Common Mistakes and Considerations
- Uninitialized Pointers: If the pointer array is not initialized properly, accessing its elements can lead to undefined behavior.
C
int *arr[3]; // Uninitialized pointer array
printf("%d", *arr[0]); // Undefined behavior, accessing uninitialized memory
- Array Size vs Pointer Array Size: The size of the pointer array should match the number of elements you intend to store in the array.
C
int *arr[5]; // Array of 5 pointers
- Dereferencing NULL Pointers: Ensure that each pointer in the array points to a valid memory location before dereferencing it.
C
int *arr[3] = {NULL, &a, &b};
printf("%d", *arr[0]); // Dereferencing a NULL pointer causes runtime error
- Summary:
- Pointer arrays store pointers to variables, and you can initialize them with memory addresses (e.g., using the & operator).
- Initialization can be done at the time of declaration or later, as long as pointers are properly assigned valid addresses.
- Pointer arithmetic can be used to navigate through elements in a pointer array, especially with arrays of strings or numeric values.
