Array Of Pointers To Strings In C

Share Your Love

In this post, we are going to learn about an array of pointers to strings In C. Array of strings means you have to store more than one string. But unfortunately, this is not the case let’s see the below example:

char sports[5][15] = {
                         "golf",
                         "hockey",
                         "football",
                         "cricket",
                         "shooting"
                     };

Then this sports array is stored inside memory as follows:

string array in 2D

You saw in the above figure all the strings are not filled up to row. That’s why the compiler fills all the cells with a null character (‘\0’).

The total size of the sports string array is 75 bytes but only 34 bytes are used, 41 bytes are wasted.

The 41 bytes you may guess not affect the program but it affects large programs while difficult to handle. So this is a considerable waste of space.

So now how can we reduce this waste space? This is called a Jagged Array. Because the rows are in different lengths in 2D array format.

C doesn’t provide Jagged array problem solving but we can remove it by an array of pointer to string.

Now, we move on:

Array Of Pointers To Strings:

An array of pointers to strings means an array of character pointers where each pointer points to the first character of a string or the base address of the string.

So, now how can we declare and initialize an array of pointers to string?

char *sports[5] = {
                      "golf",
                      "hockey",
                      "football",
                      "cricket",
                      "shooting"
                  };

Here sports an array of pointers to strings. If we initialization is done at the time of declaration we can remove the size of an array.

So the above declaration can also be written as:

char *sports[] = {
                     "golf",
                     "hockey",
                     "football",
                     "cricket",
                     "shooting"
                 };

It is important to note that for each string the base address of the first character is stored by a string pointer whose points towards char or char*.

Here arr[0] points to the base address of “golf”, similarly the arr[1] points to the base address of “hockey” stored inside the sports string pointer.

Let’s see this picture of how the pointer string points each string array base address?

String Pointer Array To Array OF String In C

In this above figure, you see how the string pointer holds the 20 bytes and the array of string which holds the memory 34 bytes. The total memory utilization is 54 bytes.

Now, you see the difference between these two 2-D array declarations above with and without using a string pointer array which is proper memory utilization and saves around 75-54 = 21 bytes of memory which is effective use.

It’s not guaranteed that all the strings are stored in contiguous memory locations but the characters within a string are stored in contiguous memory locations.

In the following program, we demonstrate how the array of strings is accessed through the pointer string array process and prints the address.

#include<stdio.h>
#include<string.h>
int main()
{
    int i = 1, *ip = &i;
    char *sports[] = {
                         "golf",
                         "hockey",
                         "football",
                         "cricket",
                         "shooting"
                     };
    for(i = 0; i < 5; i++)
    {
        printf("String = %10s", sports[i] );
        printf("\tAddress of string literal = %p\n", sports[i]);
    }
    // signal to operating system program ran fine
    return 0;
}

Output:

String =       golf     Address of string literal = 0x556a751aa008
String =     hockey     Address of string literal = 0x556a751aa00d
String =   football     Address of string literal = 0x556a751aa014
String =    cricket     Address of string literal = 0x556a751aa01d
String =   shooting     Address of string literal = 0x556a751aa025

At last in this chapter, we learn can’t assign a 2D array of strings to one 2D array. Because it is not proper memory utilization. So by string pointer array helps us this by using assignment(=) operator.

char games[3][10] = {
                        "roadrash",
                        "nfs",
                        "angrybirds"
                    };
games[0] = "hitman";   // wrong

But the above declaration is done by an array of string pointers:


char *games[3][10] = {
                        "roadrash",
                        "nfs",
                        "angrybirds"
                    };
games[0] = "hitman";  

Method 1: Pointer to the 1D array:

#include<stdio.h>
int main()
{
    int row =0;
    //create 2d array of the characters
    char arr[5][10] = {"Qwerty1", "Qwerty2", "Qwerty3", "Qwerty4", "Qwerty5"};
    //create pointer to the array
    char (*ptrArr)[10] = NULL;
    //initialize the pointer with array
    ptrArr = arr;
    for (row = 0; row < 5; ++row)// Loop for coloumn
    {
        printf("%s \n", ptrArr[row]);
    }
    return 0;
}

Output:

Qwerty1
Qwerty2
Qwerty3
Qwerty4
Qwerty5

Method 2: Pointer to the 2D array:

#include<stdio.h>
int main()
{
    int row =0;
    //create 2d array of the characters
    char arr[5][10] = {"Qwerty1", "Qwerty2", "Qwerty3", "Qwerty4", "Qwerty5"};
    //create pointer to the 2d array
    char (*ptrArr)[5][10] = NULL;
    //initialize the pointer
    ptrArr = &arr;
    for (row = 0; row < 5; ++row)// Loop for coloumb
    {
        printf("%s \n", (*ptrArr)[row]);
    }
    return 0;
}

Output:

Qwerty1
Qwerty2
Qwerty3
Qwerty4
Qwerty5

Access array of string using the pointer to the array and pointer  to pointer

Method 1: Pointer to the 1D array

#include<stdio.h>
int main()
{
    int row =0;
    //create 2d array of the characters
    char arr[5][10] = {"Qwerty1", "Qwerty2", "Qwerty3", "Qwerty4", "Qwerty5"};
    //create pointer to the array
    char * (*ptrArr)[5] = NULL;
    //initialize the pointer
    ptrArr = &arr;
    for (row = 0; row < 5; ++row)// Loop for coloumb
    {
        printf("%s \n", (*ptrArr)[row]);
    }
    return 0;
}

Output:

Qwerty1
Qwerty2
Qwerty3
Qwerty4
Qwerty5

Method 2: Pointer to pointer:

#include<stdio.h>
int main()
{
    int row =0;
    //create 2d array of the characters
    char arr[5][10] = {"Qwerty1", "Qwerty2", "Qwerty3", "Qwerty4", "Qwerty5"};
    //create pointer to the array
    char **ptr = NULL;
    //initialize the pointer with array
    ptr = arr;
    for (row = 0; row < 5; ++row)// Loop for coloumb
    {
        printf("   %s \n", ptr[row]);
    }
    return 0;
}

Output:

Qwerty1
Qwerty2
Qwerty3
Qwerty4
Qwerty5

Some Invalid Operation Of Array of Pointers To Strings:

char *top_games[5];

When the compiler sees the above statement it reserves 20 bytes of memory. So how is it reserved? The pointer holds the 5 strings base address whose size is 4 bytes then a total of 5 * 4 =20 bytes.

But it doesn’t allocate any memory for string literal. At this point, all the elements of the top_games array contain the garbage values and maybe point to anywhere in memory. This means the following operations are not valid.

scanf("%s", top_games[0]);             // invalid
strcpy(top_games[0], "mario");         // invalid
gets(top_games[0]);                    // invalid
strcat(top_games[0], "needforspeed");  // invalid

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 355

Newsletter Updates

Enter your email address below to subscribe to our newsletter