The Functioning of Array of Integer Pointers

Introduction

An array of integer pointers is declared like any other integer array, but the array name is prefixed with an asterisk * sign to indicate to the C compiler, that we are defining an array of pointers.

int *<array_name>[array_size];

After we declare an array of pointers, it will be able to store the addresses of different variables in the elements of the array. The elements of the array will point to variables defined elsewhere in the memory. It is also possible, that the elements of the array do not point to any variable as we have not defined any variables.

Consider

int *array_name[5];

array_name has five elements and each of these elements can contain the address of the memory location in which a variable is defined.

Array Name

*indicates that the elements of the array contain the address of memory variables.

Write a program to create an array of integer pointer and display integer variables using the array.

Source Code

#include <stdio.h>
#include <conio.h>
void main() {
    int *ptr_array[5];
    int first_no = 100, second_no = 200, third_no = 300, fourth_no = 400, fifth_no = 500;
    int i;
    clrscr();
    ptr_array[0] = &first_no;
    ptr_array[1] = &second_no;
    ptr_array[2] = &third_no;
    ptr_array[3] = &fourth_no;
    ptr_array[4] = &fifth_no;
    printf("Program to declare an integer array to store pointers and use the pointer array to show the variable");
    for (i = 0; i < 5; i++) {
        printf("\nThe address stored in ptr_array[%2d] is %u and it points to %3d\n", i + 1, ptr_array[i], *ptr_array[i]);
    }
    getch();
}

Output

Output

The value of the address given in the output may change from instance to instance as the address is that of the memory where the variable is stored. The following figure will explain the following of the above program.

Array

Summary

An array of integer pointers is declared similarly to a regular integer array, with the primary distinction being the addition of an asterisk (*) before the array name. This asterisk signifies to the C compiler that the array is composed of pointers to integers, rather than plain integers. By using this notation, programmers can create arrays where each element holds the address of an integer variable, enabling dynamic memory management and efficient handling of large datasets. This approach is particularly useful in scenarios that require frequent modification of the array's content or when working with functions that need to return multiple values.


Similar Articles