In this article “Arrays of Pointers in C”, we explain that instead of allocating a fixed amount of memory for each string (which often wastes space), we can use pointers to handle strings of different lengths efficiently.

Arrays of Pointers in C

Introduction

An array of pointers is a collection of pointer variables, where each pointer can store the address of a variable or string.
In the case of strings, it allows us to store multiple strings of variable lengths without wasting memory.

Example of a traditional 2D character array:

char name[4][20];

This means:

  • name can store 4 strings.
  • Each string can have up to 19 characters (plus the null character ‘\0’).
  • Total memory = 4 × 20 = 80 bytes (even if all strings are shorter).

However, since most strings are not of equal length, this method leads to memory wastage.

Efficient Way – Array of Pointers

We can solve this by declaring an array of pointers, where each pointer refers to a string of any length.

char *name[4] = {“Maharashtra”, “Goa”, “Karnataka”, “Gujarat”};

Here:

  • name[0] → “Maharashtra”
  • name[1] → “Goa”
  • name[2] → “Karnataka”
  • name[3] → “Gujarat”

This declaration allocates only the required number of bytes for each string (34 bytes total), rather than a fixed size for all.

Memory Representation

Index Pointer Points To String Value
name[0] “Maharashtra” 11 chars + ‘\0’
name[1] “Goa” 3 chars + ‘\0’
name[2] “Karnataka” 9 chars + ‘\0’
name[3] “Gujarat” 7 chars + ‘\0’

Each element of the array name holds the address of the first character of its respective string.

Example Program – Array of Pointers

#include <stdio.h>

#include <conio.h>

void main() {

    char *name[4] = {“Maharashtra”, “Goa”, “Karnataka”, “Gujarat”};

    int i;

    clrscr();

    printf(“\nArray of Pointers:\n”);

    for(i = 0; i < 4; i++) {

        printf(“%s\n”, name[i]);

    }

    getch();

}

Output:

Array of Pointers:

Maharashtra

Goa

Karnataka

Gujarat

Accessing Individual Characters

To access the jth character of the ith string, use:

*(name[i] + j)

For example, *(name[1] + 1) gives ‘o’ (the second character of “Goa”).

Arrays with rows of varying lengths are called ragged arrays, and they are better managed using pointers.

Important Difference

Declaration Meaning
*p[4] Array of 4 pointers
(*p)[4] Pointer to an array of 4 elements

Example – Using Pointers with Arrays

Program to compute the sum of all elements in an array using pointers:

#include <stdio.h>

#include <conio.h>

void main() {

    int *p, sum = 0, i;

    int data[4] = {3, 1, 5, 9};

    i = 0;

    clrscr();

    p = data; // Initialize pointer with base address of data[]

    printf(“\nElements\tValue\tAddress\n”);

    while(i < 4) {

        printf(“data[%d]\t\t%d\t%u\n”, i, *p, p);

        sum = sum + (*p);  // Accessing array element via pointer

        i++, p++;

    }

    printf(“\nSum = %d\n”, sum);

    printf(“\nAddress of data[0] = %u\n”, &data[0]);

    printf(“Current pointer p = %u\n”, p);

    getch();

}

Output:

Elements        Value       Address

data[0]         3           65516

data[1]         1           65518

data[2]         5           65520

data[3]         9           65522

Sum = 18

Address of data[0] = 65516

Current pointer p = 65524

Summary

  • An array of pointers stores the addresses of multiple strings or data items.
  • It allows handling strings of different lengths efficiently.
  • Pointer arithmetic can be used to traverse and manipulate arrays.
  • It reduces memory wastage and provides faster data access.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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