- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Initialization of Pointer Arrays in C
A pointer is a variable that stores the address of another variable. The name of the pointer variable must be prefixed by the "*" symbol. Just as in the case of a normal variable, we can also declare an "array of pointers", where each subscript of the array holds the address of an array type.
How to Initialize Array of Pointers in C?
A pointer variable can be initialized at the time of declaration, by assigning it the address of an existing variable. The following snippet shows how you can initialize a pointer −
int x = 10; int *y = &x;
By default, all the variables including the pointer variables belong to the "auto storage class". It means that a pointer variable will store an unpredictable, garbage, random memory address, which can lead to undefined behavior and potential risks to a program, such as segmentation fault errors. Hence, it should be initialized to NULL if we don’t have a specific value to store at the time of declaration.
int *ptr = NULL;
A "pointer array" stores the address in each element. The type of the array must match with the type of the target variable.
Initialize Array of Pointers Using static Keyword
You can also use the static keyword to initialize an array of pointers to store "0" in each subscript.
Example
#include <stdio.h> int main(){ static int *ptr[5]; for (int i = 0; i < 5; i++){ printf("ptr[%d] = %d\n", i, ptr[i]); } return 0; }
Output
Run the code and check its output −
ptr[0]= 0 ptr[1]= 0 ptr[2]= 0 ptr[3]= 0 ptr[4]= 0
Initialize Array of Integer Pointers
Here, we declare an array of integer pointers and store the addresses of three integer variables.
Example
#include <stdio.h> int main(){ int a = 10, b = 20, c = 30; int *ptr[3] = {&a, &b, &c}; for (int i = 0; i < 3; i++){ printf("ptr[%d]: address: %d value: %d\n", i, ptr[i], *ptr[i]); } return 0; }
Output
Run the code and check its output −
ptr[0]: address: 6422040 value: 10 ptr[1]: address: 6422036 value: 20 ptr[2]: address: 6422032 value: 30
Initialize Array of Pointer by Direct Addresses
We can store the address of each element of a normal array in the corresponding element of a pointer array.
Example
#include <stdio.h> int main(){ int arr[] = {10, 20, 30}; int *ptr[3] = {&arr[0], &arr[1], &arr[2]}; for (int i = 0; i < 3; i++){ printf("ptr[%d]: address: %d value: %d\n", i, ptr[i], *ptr[i]); } return 0; }
Output
Run the code and check its output −
ptr[0]: address: 6422032 value: 10 ptr[1]: address: 6422036 value: 20 ptr[2]: address: 6422040 value: 30
Traversing an Array with its Base Address
When we obtain the base address of an array (in this case "&arr[0]"), we can obtain the addresses of its subsequent elements, knowing that the pointer increments by the size of the data type.
Hence, just with the base address (the name of the array is the same of the address of the 0th element), we can traverse an array.
Example 1
Take a look at the following example −
#include <stdio.h> int main(){ int arr[] = {10, 20, 30}; int *ptr=arr; for (int i = 0; i < 3; i++){ printf("ptr[%d]: address: %d value: %d\n", i,ptr+i, *(ptr+i)); } return 0; }
Output
Run the code and check its output −
ptr[0]: address: 6422020 value: 10 ptr[1]: address: 6422024 value: 20 ptr[2]: address: 6422028 value: 30
Example 2: Traversing a 2D Array using a Pointer Array
In this example, we have a 2D array. The address of the 0th element of each row is stored in a pointer array. When traversing, the address stored in each element of the pointer array, that points to the 0th element of the corresponding row, each incremented to fetch the values in each row.
#include <stdio.h> int main(){ // 2d array int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, }; int ROWS = 2, COLS = 4; int i, j; // pointer int (*ptr)[4] = arr; // print the element of the array via pointer ptr for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { printf("%d ", *(ptr[i]+j)); } printf("\n"); } return 0; }
Output
When you run this code, it will produce the following output −
1 2 3 4 5 6 7 8
Example 3
We don’t really need a pointer array here, as we can use the name of this 2D array as its base pointer, and increment it row and column-wise to fetch the elements in the given 2D array −
#include <stdio.h> int main(){ // 2d array int arr[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, }; int ROWS = 2, COLS = 4; int i, j; // pointer int *ptr = arr; // print the element of the array via pointer ptr for (i = 0; i < ROWS; i++){ for (j = 0; j < COLS; j++){ printf("%d ", *(ptr + i * COLS + j)); } printf("\n"); } return 0; }
Output
The output resembles that of the previous code −
1 2 3 4 5 6 7 8
Initialize Array of Character Pointers (String)
In C programming, a string is an array of char data type. Since the name of an array also represents the address of its 0th element, a string can be declared as −
char arr[] = "Hello";
Using the pointer notation, a string is assigned to a char pointer as −
char *arr = "Hello";
We can then declare an array of char pointers to store multiple strings as follows −
char *arr[3] = {"string1", "string2", "string3", . . . };
Example
The following example has an array of char pointers that is used to store the names of computer languages −
#include <stdio.h> int main(){ char *langs [10] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" }; for(int i = 0; i < 10; i++) printf("%s\n", langs[i]); return 0; }
Output
When you run this code, it will produce the following output −
PYTHON JAVASCRIPT PHP NODE JS HTML KOTLIN C++ REACT JS RUST VBSCRIPT
In this program, "langs" is a pointer to an array of 10 strings. Therefore, if "langs[0]" points to the address 5000, then "langs + 1" will point to the address 5004 which stores the pointer to the second string.
Hence, we can also use the following variation of the loop to print the array of strings −
for (int i = 0; i < 10; i++){ printf("%s\n", *(langs + i)); }
Initialization of Dynamic Array of Pointers
You can use the malloc() function to declare and initialize an array of pointers in a dynamic way.
Example
Take a look at the following example −
#include <stdio.h> int main(){ int *arr = (int *)malloc (sizeof (int) * 5); for(int i = 0; i < 5; i++){ arr[i] = i; } for (int x = 0; x < 5; x++){ printf("%d %d\n", x, arr[x]); } return 0; }
Output
When you run this code, it will produce the following output −
0 0 1 1 2 2 3 3 4 4
You can even ask for user input and assign the values to the elements in the pointer of arrays −
for(i = 0; i < 5; i++){ scanf("%d", &x); arr[i] = x; }