Pointer to a Double Dimension Array of Characters

In this section, you are going to learn

type ( *ptr ) [ row_size ] [ col_size ];

An example is as below

char a[2][4] = { "Yes", "No" };

char (*ptr)[2][4];

ptr = &a;

Pointer to an Array when incremented, increments by size of the array to which it is pointing to !

Pointer to an Array when decremented, decrements by size of the array to which it is pointing to !

Let us see how it is done. See below

  • Step 1 : Define a double dimension array

char a[2][4] = { "Yes", "No" };
  • Step 2 : Define a pointer to an array

char (*ptr)[2][4];
  • Step 3 : Let pointer to an array, point to double dimension array

ptr = &a;
  • Step 4 : Check pointer arithmetic

printf("Before Increment : ptr = %lx\n", (unsigned long int) ptr);

ptr++;

printf("After Increment : ptr = %lx\n", (unsigned long int) ptr);
  • See full program below

#include <stdio.h>

int main(void)
{
        char a[2][4] = { "Yes", "No" };

        char (*ptr)[2][4];

        ptr = &a;

        printf("Before Increment : ptr = %lx\n", (unsigned long int) ptr);

        ptr++;

        printf("After Increment : ptr = %lx\n", (unsigned long int) ptr);

        return 0;
}
  • Output is as below

Before Increment : ptr = 7ffd4a34b670
After  Increment : ptr = 7ffd4a34b678

Observe that difference is 8 !

  • Size of two dimensional array is 8 Bytes

  • Hence incrementing ptr will increment by 8 Bytes

When Pointer to an Array is passed as argument, Prototype of function should match the type properly !

Let us see how it is done. See below

  • Step 1 : Define a double dimension array

char a[2][4] = { "Yes", "No" };
  • Step 2 : Define a pointer to an array

char (*ptr)[2][4];
  • Step 3 : Let pointer to an array, point to double dimension array

ptr = &a;
  • Step 4 : Pass ptr as argument to function fun

fun(ptr);
  • Step 5 : Define function fun

void fun( char (*p)[2][4] )
{
}
  • Step 6 : Print individual strings inside fun

void fun( char (*p)[2][4] )
{
        //print strings
        printf("Print strings using %%s\n");
        for (int i = 0; i < 2; i++)
        {
                printf("%s\n", (*p)[i]);
        }
}

This is easy to understand ! Let us derive the rules

  • Rule 1 : Base Rule inside main

ptr = &a
  • Rule 2 : Base Rule inside fun

p = ptr
  • Rule 3 : From Rule 1 and Rule 2

p = &a
  • Rule 4 : Move & from RHS to LHS. This becomes * on LHS

*p = a;
  • Rule 5 : a is equal to &a[0]

*p = &a[0]
  • Rule 6 : Move & from RHS to LHS. This becomes * on LHS

**p = a[0]
  • Rule 7 : * and [ ] can be used interchangeably

(*p)[0] = a[0]
  • Rule 8 : Extending Rule 7

(*p)[i] = a[i]
  • Note that a[i] is a string (Array of characters)

  • Hence (*p)[i] is also a string (Array of characters from Rule 8)

Another way to understand !

  • char (*p)[2][4] is the declaration of pointer p

  • (*p)[ ][ ] is dereferenced to maximum level. Hence is a character

  • (*p)[ ] is dereferenced to (N - 1)th level. Hence is an array of characters

  • Step 7 : Print individual characters inside fun

void fun( char (*p)[2][4] )
{
        //print Individual characters
        printf("Print characters using %%c\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        printf("%c", (*p)[i][j]);
                }
                printf("\n");
        }
}
  • See full program below

#include <stdio.h>

void fun( char (*p)[2][4] )
{
        //print strings
        printf("Print strings using %%s\n");
        for (int i = 0; i < 2; i++)
        {
                printf("%s\n", (*p)[i]);
        }

        printf("\n");

        //print Individual characters
        printf("Print characters using %%c\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        printf("%c", (*p)[i][j]);
                }
                printf("\n");
        }
}

int main(void)
{
        char a[2][4] = { "Yes", "No" };

        char (*ptr)[2][4];

        ptr = &a;

        fun(ptr);

        return 0;
}
  • Output is as below

Print strings using %s
Yes
No

Print characters using %c
Yes
No

When Pointer to an Array is passed as argument by call by reference, Prototype of function should match the type properly !

Let us see how it is done. See below

  • Step 1 : Define a Double dimension array

char a[3][10] = { "Laptop", "Keyboard", "Mouse" };
  • Step 2 : Define a pointer to an array

char (*ptr)[3][10];
  • Step 3 : Let pointer to an array, point to double dimension array

ptr = &a;
  • Step 4 : Pass ptr as argument to function fun. Call by reference

fun(&ptr);
  • Step 5 : Define function fun and print individual strings

void fun(char (**p)[3][10])
{
        for (int i = 0; i < 3; i++) {
                printf("%s\n", (**p)[i]);
        }
}
  • Step 6 : Define function fun and print individual characters

void fun(char (**p)[3][10])
{
        for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 10; j++) {
                        printf("%c", (**p)[i][j]);
                }
                printf("\n");
        }
}

This is easy to understand ! Let us derive the rules

  • Rule 1 : Base rule in main

ptr = &a;
  • Rule 2 : Base rule in fun

p = &ptr
  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*p = ptr
  • Rule 3 : Replace ptr referring to Rule 1

*p = &a
  • Rule 4 : Move & from RHS to LHS. This becomes * on LHS

**p = a
  • Rule 5 : * and [ ] can be used interchangeably

(*p)[0] = a
  • Rule 6 : a is equal to &a[0]

(*p)[0] = &a[0]
  • Rule 7 : Move & from RHS to LHS. This becomes * on LHS

(**p)[0] = a[0]
  • Rule 8 : Extending Rule 7

(**p)[i] = a[i]

Hence we proved if a[i] is a string then (**p)[i] is also a string

  • Rule 9 : a[0] is equal to &a[0][0]

(**p)[0] = &a[0][0]
  • Rule 10 : Move & from RHS to LHS. This becomes * on LHS

(***p)[0] = a[0][0]
  • Rule 11 : * and [ ] can be used interchangeably

(**p)[0][0] = a[0][0]
  • Rule 12 : Extending Rule 11

(**p)[i][j] = a[i][j]

Hence we proved if a[i][j] is a character then (**p)[i][j] is also a character

  • See full program below

#include <stdio.h>

void fun(char (**p)[3][10])
{
        // Print strings
        printf("Print string using %%s\n");
        for (int i = 0; i < 3; i++) {
                printf("%s\n", (**p)[i]);
        }

        printf("\n");

        // Print individual characters
        printf("Print characters using %%c\n");
        for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 10; j++) {
                        printf("%c", (**p)[i][j]);
                }
                printf("\n");
        }
}

int main(void)
{
        char a[3][10] = { "Laptop", "Keyboard", "Mouse" };

        char (*ptr)[3][10];

        ptr = &a;

        fun(&ptr);

        return 0;
}
  • Output is as below

Print string using %s
Laptop
Keyboard
Mouse

Print characters using %c
Laptop
Keyboard
Mouse
  • Step 1 : Define a triple dimension array of characters

char a[3][4][5] = {
        { "Lap0", "Key0", "Mou0", "tou0" },
        { "Lap1", "Key1", "Mou1", "tou1" },
        { "Lap2", "Key2", "Mou2", "tou2" },
};
  • Step 2 : Define a pointer to an array of double dimension

char (*ptr)[4][5];
  • Step 3 : Let the pointer to point to Double dimension array inside a Double dimension array

ptr = &a[0];
  • Step 4 : Print the strings using %s : Method 1

for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
                printf("ptr[%d][%d] = %s\n", i, j, ptr[i][j]);
        }
}
  • Step 5 : Print the strings using %s : Method 2

for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
                printf("(*ptr)[%d] = %s\n", j, (*ptr)[j]);
        }
        ptr++;
}

This is easy to understand ! Let us derive the rules

  • Rule 1 : Base rule

ptr = &a[0];
  • Rule 2 : Move & from RHS to LHS. This becomes * on LHS

*ptr = a[0]
  • Rule 3 : a[0] is equal to &a[0][0]

*ptr = &a[0][0]
  • Rule 4 : Move & from RHS to LHS. This becomes * on LHS

**ptr = a[0][0]
  • Rule 5 : * and [ ] can be used interchangeably

ptr[0][0] = a[0][0]
  • Rule 6 : Extending Rule 3

ptr[i][j] = a[i][j]

Hence we proved, if a[i][j] is a string then p[i][j] is also a string

  • See full program below

#include <stdio.h>

int main(void)
{
        char a[3][4][5] = {
                { "Lap0", "Key0", "Mou0", "tou0" },
                { "Lap1", "Key1", "Mou1", "tou1" },
                { "Lap2", "Key2", "Mou2", "tou2" },
        };

        char (*ptr)[4][5];

        ptr = &a[0];

        //Method 1 : print strings
        for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 4; j++) {
                        printf("ptr[%d][%d] = %s\n", i, j, ptr[i][j]);
                }
        }

        printf("\n");

        //Method 2 : print strings
        for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 4; j++) {
                        printf("(*ptr)[%d] = %s\n", j, (*ptr)[j]);
                }
                ptr++;
        }

        return 0;
}
  • Output is as below

ptr[0][0] = Lap0
ptr[0][1] = Key0
ptr[0][2] = Mou0
ptr[0][3] = tou0
ptr[1][0] = Lap1
ptr[1][1] = Key1
ptr[1][2] = Mou1
ptr[1][3] = tou1
ptr[2][0] = Lap2
ptr[2][1] = Key2
ptr[2][2] = Mou2
ptr[2][3] = tou2

(*ptr)[0] = Lap0
(*ptr)[1] = Key0
(*ptr)[2] = Mou0
(*ptr)[3] = tou0
(*ptr)[0] = Lap1
(*ptr)[1] = Key1
(*ptr)[2] = Mou1
(*ptr)[3] = tou1
(*ptr)[0] = Lap2
(*ptr)[1] = Key2
(*ptr)[2] = Mou2
(*ptr)[3] = tou2