Initialization of Pointer Arrays in C Programming
Discover how to initialize an array of pointers in C programming. Learn how pointers store the addresses of variables and how you can use them effectively by creating arrays of pointers. This guide covers the initialization process with examples, helping you to manage memory addresses efficiently in your C programs.
Initialization of Pointer Arrays in C
A pointer is a special type of variable that stores the address of another variable. To define a pointer variable, you use the *
symbol before the variable name. Just like regular variables, you can also create an array of pointers, where each index in the array holds the address of an array type.
How to Initialize an Array of Pointers in C?
You can initialize a pointer variable at the time of its declaration by assigning it the address of an existing variable. Here's an example:
Syntax
int x = 25;
int *y = &x;
By default, all variables, including pointer variables, belong to the auto storage class. This means a pointer variable may store an unpredictable or random memory address, which can lead to undefined behavior, such as segmentation faults. Therefore, it's good practice to initialize pointers to NULL
if you don’t have a specific value to assign at the time of declaration:
Syntax
int *ptr = NULL;
An array of pointers stores addresses in each of its elements. The type of the array should match the type of the target variable.
Initialize Array of Pointers Using the Static Keyword
You can also use the static
keyword to initialize an array of pointers, which will automatically set each element to 0
.
Example
Example Code
#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
ptr[0] = 0
ptr[1] = 0
ptr[2] = 0
ptr[3] = 0
ptr[4] = 0
Initialize Array of Integer Pointers
In this example, we declare an array of integer pointers and store the addresses of three integer variables.
Example
Example Code
#include <stdio.h>
int main() {
int a = 15, b = 25, c = 35;
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
ptr[0]: address: 6422050 value: 15
ptr[1]: address: 6422046 value: 25
ptr[2]: address: 6422042 value: 35
Initialize Array of Pointers by Direct Addresses
You can store the address of each element of a normal array in the corresponding element of a pointer array.
Example
Example Code
#include <stdio.h>
int main() {
int arr[] = {15, 25, 35};
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
ptr[0]: address: 6422042 value: 15
ptr[1]: address: 6422046 value: 25
ptr[2]: address: 6422050 value: 35
Traversing an Array with Its Base Address
When you obtain the base address of an array (for example, &arr[0]
), you can access the addresses of its subsequent elements since the pointer increments by the size of the data type.
Thus, with just the base address (the name of the array represents the address of its 0th element), you can traverse the array.
Example 1
Here's an example:
Example Code
#include <stdio.h>
int main() {
int arr[] = {15, 25, 35};
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
ptr[0]: address: 6422036 value: 15
ptr[1]: address: 6422040 value: 25
ptr[2]: address: 6422044 value: 35
Example 2: Traversing a 2D Array Using a Pointer Array
In this example, we have a 2D array. The address of the first element of each row is stored in a pointer array. When traversing, we can use the address stored in each element of the pointer array, which points to the first element of the corresponding row, incremented to fetch the values in each row.
Example Code
#include <stdio.h>
int main() {
// 2D array
int arr[3][4] = {
{10, 20, 30, 40},
{50, 60, 70, 80},
};
int ROWS = 2, COLS = 4;
int i, j;
// Pointer
int (*ptr)[4] = arr;
// Print the elements 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
10 20 30 40
50 60 70 80
Example 3
In this case, we don't need a pointer array, as we can use the name of this 2D array as its base pointer, incrementing it row and column-wise to access the elements in the given 2D array:
Example Code
#include <stdio.h>
int main() {
// 2D array
int arr[3][4] = {
{10, 20, 30, 40},
{50, 60, 70, 80},
};
int ROWS = 2, COLS = 4;
int i, j;
// Pointer
int *ptr = arr;
// Print the elements 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
10 20 30 40
50 60 70 80
Initialize Array of Character Pointers (String)
In C, a string is an array of characters. Since the name of an array also represents the address of its 0th element, you can declare a string like this:
Syntax
char arr[] = "Goodbye";
Using pointer notation, you can assign a string to a character pointer like this:
Syntax
char *arr = "Goodbye";
You can then declare an array of character pointers to store multiple strings:
Syntax
char *arr[3] = {"string1", "string2", "string3"};
Example
Here’s an example with an array of character pointers to store names of programming languages:
Example Code
#include <stdio.h>
int main() {
char *langs[10] = {
"JAVA", "RUST", "GO",
"SWIFT", "HTML", "KOTLIN", "C#",
"REACT", "NODE", "PHP"
};
for (int i = 0; i < 10; i++) {
printf("%s\n", langs[i]);
}
return 0;
}
Output
JAVA
RUST
GO
SWIFT
HTML
KOTLIN
C#
REACT
NODE
PHP
In this program, langs
is a pointer to an array of 10 strings. If langs[0]
points to address 5000, then langs + 1
will point to address 5004, which stores the pointer to the second string. Therefore, we can also use the following loop variation to print the array of strings:
Example Code
for (int i = 0; i < 10; i++) {
printf("%s\n", *(langs + i));
}
Initialization of Dynamic Array of Pointers
You can also use the malloc()
function to declare and initialize an array of pointers dynamically.
Example
Example Code
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)malloc(sizeof(int) * 5);
for (int i = 0; i < 5; i++) {
arr[i] = i + 5;
}
for (int x = 0; x < 5; x++) {
printf("%d %d\n", x, arr[x]);
}
free(arr); // Free the allocated memory
return 0;
}
Output
0 5
1 6
2 7
3 8
4 9
You can also request user input to assign values to the elements in the pointer array:
Example Code
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}