C – Initialization of Pointer Arrays


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] = %dn", 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: %dn", 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: %dn", 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: %dn", 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("%sn", 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("%sn", *(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 %dn", 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;
}

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *