Functions and Character Triple Pointer

In this section, you are going to learn

What are the calling conventions of character triple pointer ?

Call by Value

Call by Reference

char ***tp;

Consider a character triple pointer

char ***tp;

Let us answer few basic questions about character triple pointer

If fun(x) is the function call, then fun(typeof(x)) is the prototype / definition

Function Call

Function Definition

Observations

fun(tp[0][0][0])

void fun(char x) {}

  • Call by Value

fun(tp[1][0][0])

void fun(char x) {}

  • Call by Value

fun(tp[2][0][0])

void fun(char x) {}

  • Call by Value

fun(&tp[0][0][0])

void fun(char *p) {}

  • Call by Reference

fun(&tp[1][0][0])

void fun(char *p) {}

  • Call by Reference

fun(&tp[2][0][0])

void fun(char *p) {}

  • Call by Reference

fun(tp[0][0])

void fun(char *p) {}

  • Call by Reference

fun(tp[1][0])

void fun(char *p) {}

  • Call by Reference

fun(tp[2][0])

void fun(char *p) {}

  • Call by Reference

fun(&tp[0][0])

void fun(char **p) {}

  • Call by Reference

fun(&tp[1][0])

void fun(char **p) {}

  • Call by Reference

fun(&tp[2][0])

void fun(char **p) {}

  • Call by Reference

fun(**tp)

void fun(char *p) {}

  • Call by Reference

fun(*(*(tp + 1) + 0))

void fun(char *p) {}

  • Call by Reference

fun(*(*(tp + 2) + 0))

void fun(char *p) {}

  • Call by Reference

fun(tp[0])

void fun(char **p) {}

  • Call by Reference

fun(tp[1])

void fun(char **p) {}

  • Call by Reference

fun(tp[2])

void fun(char **p) {}

  • Call by Reference

fun(&tp[0])

void fun(char ***p) {}

  • Call by Reference

fun(*tp)

void fun(char **p) {}

  • Call by Reference

fun(*(tp + 1))

void fun(char **p) {}

  • Call by Reference

fun(*(tp + 2))

void fun(char **p) {}

  • Call by Reference

fun(tp)

void fun(char ***p) {}

  • Call by Reference

fun(&tp)

void fun(char ****p) {}

  • Call by Reference

Let us understand the reason behind above prototypes !

If Declaration has THREE dereference operators, and

  • Expression has THREE dereference operators * * *, and

  • Expression does not have &

  • then it is call by value

If Declaration has THREE dereference operators, and

  • Expression has THREE dereference operators * * [ ], and

  • Expression does not have &

  • then it is call by value

If Declaration has THREE dereference operators, and

  • Expression has THREE dereference operators * [ ] [ ], and

  • Expression does not have &

  • then it is call by value

If Declaration has THREE dereference operators, and

  • Expression has THREE dereference operators [] [] [], and

  • Expression does not have &

  • then it is call by value

Let us look at examples

  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");

Condition 1 : Declaration has THREE dereference operators [ ], [ ] and [ ]

  • Step 2 : Consider an expression tp[1][1][1]

Condition 2 : Expression has THREE dereference operators [ ], [ ] and [ ]

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence tp[1][1][1] is Call By Value

  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");

Condition 1 : Declaration has THREE dereference operators [ ], [ ] and [ ]

  • Step 2 : Consider an expression ***tp

Condition 2 : Expression has THREE dereference operators *, * and *

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence ***tp is Call By Value

If Declaration has THREE dereference operators, and

  • Expression has THREE dereference operators * * * OR * * [] OR * [] [] OR [] [] [] and

  • Expression has &

  • then it is call by reference

  • Example : &tp[0][0][0], &tp[1][2][3]

If Declaration has THREE dereference operators, and

  • Expression has TWO dereference operator * * OR [] [] OR * []

  • then it is call by reference

  • Example : tp[0][0]

If Declaration has THREE dereference operators, and

  • Expression has ONE dereference operators, * OR [ ]

  • then it is call by reference

  • Example : tp[0]

If Declaration has THREE dereference operators, and

  • Expression has ZERO dereference operators

  • then it is call by reference

  • Example : tp

  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");

Condition 1 : Declaration has THREE dereference operators [ ] [ ] and [ ]

  • Step 2 : Consider an expression &tp[1][1][1]

Condition 2 : Expression has THREE dereference operators [ ] [ ] and [ ]

Note : [ ] and * are dereference operators

Condition 3 : Expression has & operator

Hence &tp[1][1][1] is Call By Reference

  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");

Condition 1 : Declaration has THREE dereference operators [ ] [ ] and [ ]

  • Step 2 : Consider an expression tp[1]

Condition 2 : Expression has ONE dereference operator

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence tp[1] is Call By Reference

Let us look at examples of Call by Value

  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");
  • Step 2 : Pass tp[0][0][0], tp[1][0][0], tp[2][0][0] to a function fun

fun(tp[0][0][0]);

fun(tp[1][0][0]);

fun(tp[2][0][0]);
  • Step 3 : Define function fun

void fun(char c)
{
        printf("char is %c\n", c);

        c = 'k';
}
  • Step 4 : Note that it is call by Value for below reason

Condition 1 : Declaration has THREE dereference operators [ ] [ ] and [ ]

Condition 2 : Expression has THREE dereference operators [ ] [ ] and [ ]

Condition 3 : Expression DOES NOT have & operator

Means changing value of character inside function DOES NOT affect value of character in Caller !

  • Step 5 : Free memory after use

for (int i = 0; i < 3; i++)
{
        for (int j = 0; j < 4; j++)
        {
                free( tp[i][j] );
        }
        free( tp[i] );
}

free(tp);
  • See full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void fun(char c)
{
        printf("char is %c\n", c);

        c = 'k';
}

int main(void)
{
        char ***tp;

        tp = malloc( 3 * sizeof(char **) );

        for (int i = 0; i < 3; i++)
        {
                tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
                for (int j = 0; j < 4; j++)
                {
                        tp[i][j] = malloc( 5 * sizeof(char) );
                }
        }

        strcpy(tp[0][0], "lap0");
        strcpy(tp[0][1], "top0");
        strcpy(tp[0][2], "xxx0");
        strcpy(tp[0][3], "1230");

        strcpy(tp[1][0], "gap0");
        strcpy(tp[1][1], "pop0");
        strcpy(tp[1][2], "yyy0");
        strcpy(tp[1][3], "4560");

        strcpy(tp[2][0], "sap0");
        strcpy(tp[2][1], "gop0");
        strcpy(tp[2][2], "zzz0");
        strcpy(tp[2][3], "7890");

        fun(tp[0][0][0]);

        fun(tp[1][0][0]);

        fun(tp[2][0][0]);

        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        free( tp[i][j] );
                }
                free( tp[i] );
        }

        free(tp);

        return 0;
}
  • Output is as below

char is l
char is g
char is s
  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");
  • Step 2 : Pass ***tp, *(*(*(tp + 1) + 0) + 0), *(*(*(tp + 2) + 0) + 0) to a function fun

fun( ***tp );

fun( *(*(*(tp + 1) + 0) + 0) );

fun( *(*(*(tp + 2) + 0) + 0) );
  • Step 3 : Define function fun

void fun(char c)
{
        printf("char is %c\n", c);

        c = 'k';
}
  • Step 4 : Note that it is call by Value for below reason

Condition 1 : Declaration has THREE dereference operators [ ] [ ] and [ ]

Condition 2 : Expression has THREE dereference operators * * and *

Condition 3 : Expression DOES NOT have & operator

Means changing value of character inside function DOES NOT affect value of character in Caller !

  • Step 5 : Free memory after use

for (int i = 0; i < 3; i++)
{
        for (int j = 0; j < 4; j++)
        {
                free( tp[i][j] );
        }
        free( tp[i] );
}

free(tp);
  • See full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void fun(char c)
{
        printf("char is %c\n", c);
}

int main(void)
{
        char ***tp;

        tp = malloc( 3 * sizeof(char **) );

        for (int i = 0; i < 3; i++)
        {
                tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
                for (int j = 0; j < 4; j++)
                {
                        tp[i][j] = malloc( 5 * sizeof(char) );
                }
        }

        strcpy(tp[0][0], "lap0");
        strcpy(tp[0][1], "top0");
        strcpy(tp[0][2], "xxx0");
        strcpy(tp[0][3], "1230");

        strcpy(tp[1][0], "gap0");
        strcpy(tp[1][1], "pop0");
        strcpy(tp[1][2], "yyy0");
        strcpy(tp[1][3], "4560");

        strcpy(tp[2][0], "sap0");
        strcpy(tp[2][1], "gop0");
        strcpy(tp[2][2], "zzz0");
        strcpy(tp[2][3], "7890");

        fun( ***tp );

        fun( *(*(*(tp + 1) + 0) + 0) );

        fun( *(*(*(tp + 2) + 0) + 0) );


        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        free( tp[i][j] );
                }
                free( tp[i] );
        }

        free(tp);

        return 0;
}
  • Output is as below

char is l
char is g
char is s

Let us look at examples of Call by Reference

  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");

There are 12 single dimension arrays in char \*\*\*tp with respect to above allocation

tp[0][0]

tp[0][1]

tp[0][2]

tp[0][3]

tp[1][0]

tp[1][1]

tp[1][2]

tp[1][3]

tp[2][0]

tp[2][1]

tp[2][2]

tp[2][3]

  • Step 2.1 : Method 1 : Pass tp[0][0], tp[1][0], tp[2][0] to a function fun

fun( tp[0][0] );

fun( tp[1][0] );

fun( tp[2][0] );
  • Step 2.2 : Method 2 : Pass &tp[0][0][0], &tp[1][0][0], &tp[2][0][0] to a function fun

fun( &tp[0][0][0] );

fun( &tp[1][0][0] );

fun( &tp[2][0][0] );
  • Step 2.3 : Method 3 : Pass **tp, *(*(tp + 1) + 0), *(*(tp + 2) + 0) to a function fun

fun( **tp );

fun( *(*(tp + 1) + 0) );

fun( *(*(tp + 2) + 0) );
  • Step 3.1 : Define function fun

void fun(char *ptr)
{
        printf("string is %s\n", ptr);
}
  • Step 4 : Note that it is call by Reference. Means contents of single dimension array can be changed inside function fun

void fun(char *ptr)
{
        strcpy(ptr, "123");
}
  • Step 5 : Free memory after use

for (int i = 0; i < 3; i++)
{
        for (int j = 0; j < 4; j++)
        {
                free( tp[i][j] );
        }
        free( tp[i] );
}

free(tp);
  • See full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void fun(char *ptr)
{
        printf("string is %s\n", ptr);
}

int main(void)
{
        char ***tp;

        tp = malloc( 3 * sizeof(char **) );

        for (int i = 0; i < 3; i++)
        {
                tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
                for (int j = 0; j < 4; j++)
                {
                        tp[i][j] = malloc( 5 * sizeof(char) );
                }
        }

        strcpy(tp[0][0], "lap0");
        strcpy(tp[0][1], "top0");
        strcpy(tp[0][2], "xxx0");
        strcpy(tp[0][3], "1230");

        strcpy(tp[1][0], "gap0");
        strcpy(tp[1][1], "pop0");
        strcpy(tp[1][2], "yyy0");
        strcpy(tp[1][3], "4560");

        strcpy(tp[2][0], "sap0");
        strcpy(tp[2][1], "gop0");
        strcpy(tp[2][2], "zzz0");
        strcpy(tp[2][3], "7890");

        printf("Method 1 : Access Single dimension arrays\n");

        fun( tp[0][0] );

        fun( tp[1][0] );

        fun( tp[2][0] );

        printf("Method 2 : Access Single dimension arrays\n");

        fun( &tp[0][0][0] );

        fun( &tp[1][0][0] );

        fun( &tp[2][0][0] );

        printf("Method 3 : Access Single dimension arrays\n");

        fun( **tp );

        fun( *(*(tp + 1) + 0) );

        fun( *(*(tp + 2) + 0) );

        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        free( tp[i][j] );
                }
                free( tp[i] );
        }

        free(tp);

        return 0;
}
  • Output is as below

Method 1 : Access Single dimension arrays
string is lap0
string is gap0
string is sap0

Method 2 : Access Single dimension arrays
string is lap0
string is gap0
string is sap0

Method 3 : Access Single dimension arrays
string is lap0
string is gap0
string is sap0
  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");

There are 12 single dimension arrays in

tp[0][0]

tp[0][1]

tp[0][2]

tp[0][3]

tp[1][0]

tp[1][1]

tp[1][2]

tp[1][3]

tp[2][0]

tp[2][1]

tp[2][2]

tp[2][3]

Address of single dimension arrays is simply

&tp[0][0]

&tp[0][1]

&tp[0][2]

&tp[0][3]

&tp[1][0]

&tp[1][1]

&tp[1][2]

&tp[1][3]

&tp[2][0]

&tp[2][1]

&tp[2][2]

&tp[2][3]

  • Step 2.1 : Method 1 : Pass address of single dimension arrays to a function fun

fun( &tp[0][0] );

fun( &tp[1][0] );

fun( &tp[2][0] );
  • Step 2.2 : Method 2 : Pass address of single dimension arrays to a function fun

fun( tp[0] );

fun( tp[1] );

fun( tp[2] );
  • Step 2.3 : Method 2 : Pass address of single dimension arrays to a function fun

fun( *tp );

fun( *(tp + 1) );

fun( *(tp + 2) );
  • Step 3.1 : Define the function fun

void fun(char **ptr )
{
        printf("string is %s\n", *ptr);
}
  • Step 3.2 : Define the function fun to change the contents of single dimension array

void fun(char **ptr )
{
        printf("string is %s\n", *ptr);

        strcpy((char *)*ptr, "Gap");
}
  • Step 3.3 : Define the function fun to change the contents of single dimension array character by character

void fun(char **ptr)
{
        strcpy((char *)*ptr, "Gap");

        (*ptr)[0] = 'p';
        (*ptr)[1] = 'g';
        (*ptr)[2] = 'x';
        (*ptr)[3] = 'y';
        (*ptr)[4] = '\0';

        printf("string is %s\n", *ptr);
}
  • Step 4 : Free memory after use

for (int i = 0; i < 3; i++)
{
        for (int j = 0; j < 4; j++)
        {
                free( tp[i][j] );
        }
        free( tp[i] );
}

free(tp);
  • See full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void fun(char **ptr )
{
        strcpy((char *)*ptr, "Gap");

        (*ptr)[0] = 'p';
        (*ptr)[1] = 'g';
        (*ptr)[2] = 'x';
        (*ptr)[3] = 'y';
        (*ptr)[4] = '\0';

        printf("string is %s\n", *ptr);
}

int main(void)
{
        char ***tp;

        tp = malloc( 3 * sizeof(char **) );

        for (int i = 0; i < 3; i++)
        {
                tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
                for (int j = 0; j < 4; j++)
                {
                        tp[i][j] = malloc( 5 * sizeof(char) );
                }
        }

        strcpy(tp[0][0], "lap0");
        strcpy(tp[0][1], "top0");
        strcpy(tp[0][2], "xxx0");
        strcpy(tp[0][3], "1230");

        strcpy(tp[1][0], "gap0");
        strcpy(tp[1][1], "pop0");
        strcpy(tp[1][2], "yyy0");
        strcpy(tp[1][3], "4560");

        strcpy(tp[2][0], "sap0");
        strcpy(tp[2][1], "gop0");
        strcpy(tp[2][2], "zzz0");
        strcpy(tp[2][3], "7890");

        printf("Method 1 : Access Single dimension arrays\n");

        fun( &tp[0][0] );

        fun( &tp[1][0] );

        fun( &tp[2][0] );

        printf("Method 2 : Access Single dimension arrays\n");

        fun( tp[0] );

        fun( tp[1] );

        fun( tp[2] );

        printf("Method 3 : Access Single dimension arrays\n");

        fun( *tp );

        fun( *(tp + 1) );

        fun( *(tp + 2) );

        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        free( tp[i][j] );
                }
                free( tp[i] );
        }

        free(tp);

        return 0;
}
  • Output is as below

Method 1 : Access Single dimension arrays
string is pgxy
string is pgxy
string is pgxy

Method 2 : Access Single dimension arrays
string is pgxy
string is pgxy
string is pgxy

Method 3 : Access Single dimension arrays
string is pgxy
string is pgxy
string is pgxy
  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");
  • Step 2 : Pass Double dimension array to function fun

fun(tp[1]);
  • Step 3.1 : Define function fun

void fun(char **ptr)
{

}
  • Step 3.2 : Access and Print strings inside function fun

void fun(char **ptr)
{
        for(int i = 0; i < 4; i++)
        {
                printf("%s", ptr[i]);
                printf("\n");
        }
}
  • Step 3.3 : Access and Print characters inside function fun

void fun(char **ptr)
{
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5; j++)
                {
                        printf("%c", ptr[i][j]);
                }
                printf("\n");
        }
}
  • Step 3.4 : Access and Change strings inside function fun

void fun(char **ptr)
{
        for(int i = 0; i < 4; i++)
        {
                strncpy(ptr[i], "gggg", 5);
        }
}
  • Step 3.5 : Access and Change characters inside function fun

void fun(char **ptr)
{
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5 - 1; j++)
                {
                        ptr[i][j] = 'c';
                }
        }
}
  • Step 4 : Free memory after use

for (int i = 0; i < 3; i++)
{
        for (int j = 0; j < 4; j++)
        {
                free( tp[i][j] );
        }
        free( tp[i] );
}

free(tp);
  • Step 5 : See the full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void fun(char **ptr)
{
        // Access Strings and Print
        printf("fun() : Access Strings and Print\n");
        for(int i = 0; i < 4; i++)
        {
                printf("%s", ptr[i]);
                printf("\n");
        }

        // Access Characters and Print
        printf("fun() : Access Characters and Print\n");
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5; j++)
                {
                        printf("%c", ptr[i][j]);
                }

                printf("\n");
        }

        //Access Strings and Change
        for(int i = 0; i < 4; i++)
        {
                strncpy(ptr[i], "gggg", 5);
        }

        printf("fun() : Access Strings and Print\n");
        for(int i = 0; i < 4; i++)
        {
                printf("%s", ptr[i]);
                printf("\n");
        }

        // Access Characters and Change
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5 - 1; j++)
                {
                        ptr[i][j] = 'c';
                }
        }

        printf("fun() : Access Strings and Print\n");
        for(int i = 0; i < 4; i++)
        {
                printf("%s", ptr[i]);
                printf("\n");
        }
}

int main(void)
{
        char ***tp;

        tp = malloc( 3 * sizeof(char **) );

        for (int i = 0; i < 3; i++)
        {
                tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
                for (int j = 0; j < 4; j++)
                {
                        tp[i][j] = malloc( 5 * sizeof(char) );
                }
        }

        strcpy(tp[0][0], "lap0");
        strcpy(tp[0][1], "top0");
        strcpy(tp[0][2], "xxx0");
        strcpy(tp[0][3], "1230");

        strcpy(tp[1][0], "gap0");
        strcpy(tp[1][1], "pop0");
        strcpy(tp[1][2], "yyy0");
        strcpy(tp[1][3], "4560");

        strcpy(tp[2][0], "sap0");
        strcpy(tp[2][1], "gop0");
        strcpy(tp[2][2], "zzz0");
        strcpy(tp[2][3], "7890");

        fun(tp[1]);

        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        free( tp[i][j] );
                }
                free( tp[i] );
        }

        return 0;
}
  • Step 5 : Output is as below

fun() : Access Strings and Print
gap0
pop0
yyy0
4560

fun() : Access Characters and Print
gap0
pop0
yyy0
4560

fun() : Access Strings and Print
gggg
gggg
gggg
gggg

fun() : Access Strings and Print
cccc
cccc
cccc
cccc
  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");
  • Step 2 : Pass Address of Double dimension array to function fun

fun(&tp[1]);
  • Step 3.1 : Define function fun

void fun(char ***ptr)
{

}
  • Step 3.2 : Access and Print strings inside function fun

void fun(char ***ptr)
{
        for(int i = 0; i < 4; i++)
        {
                printf("%s", (*ptr)[i]);
                printf("\n");
        }
}
  • Step 3.3 : Access and Print characters inside function fun

void fun(char ***ptr)
{
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5; j++)
                {
                        printf("%c", (*ptr)[i][j]);
                }
                printf("\n");
        }
}
  • Step 3.4 : Access and Change strings inside function fun

void fun(char ***ptr)
{
        for(int i = 0; i < 4; i++)
        {
                strncpy( (*ptr)[i], "gggg", 5);
        }
}
  • Step 3.5 : Access and Change characters inside function fun

void fun(char ***ptr)
{
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5 - 1; j++)
                {
                        (*ptr)[i][j] = 'c';
                }
        }
}
  • Step 4 : Free memory after use

for (int i = 0; i < 3; i++)
{
        for (int j = 0; j < 4; j++)
        {
                free( tp[i][j] );
        }
        free( tp[i] );
}

free(tp);
  • Step 5 : See the full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void fun(char ***ptr)
{
        // Access Strings and Print
        printf("fun() : Access Strings and Print\n");
        for(int i = 0; i < 4; i++)
        {
                printf("%s", (*ptr)[i]);
                printf("\n");
        }

        // Access Characters and Print
        printf("fun() : Access Characters and Print\n");
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5; j++)
                {
                        printf("%c", (*ptr)[i][j]);
                }

                printf("\n");
        }

        //Access Strings and Change
        for(int i = 0; i < 4; i++)
        {
                strncpy((*ptr)[i], "gggg", 5);
        }

        printf("fun() : Access Strings and Print\n");
        for(int i = 0; i < 4; i++)
        {
                printf("%s", (*ptr)[i]);
                printf("\n");
        }

        // Access Characters and Change
        for(int i = 0; i < 4; i++)
        {
                for(int j = 0; j < 5 - 1; j++)
                {
                        (*ptr)[i][j] = 'c';
                }
        }

        printf("fun() : Access Strings and Print\n");
        for(int i = 0; i < 4; i++)
        {
                printf("%s", (*ptr)[i]);
                printf("\n");
        }
}

int main(void)
{
        char ***tp;

        tp = malloc( 3 * sizeof(char **) );

        for (int i = 0; i < 3; i++)
        {
                tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
                for (int j = 0; j < 4; j++)
                {
                        tp[i][j] = malloc( 5 * sizeof(char) );
                }
        }

        strcpy(tp[0][0], "lap0");
        strcpy(tp[0][1], "top0");
        strcpy(tp[0][2], "xxx0");
        strcpy(tp[0][3], "1230");

        strcpy(tp[1][0], "gap0");
        strcpy(tp[1][1], "pop0");
        strcpy(tp[1][2], "yyy0");
        strcpy(tp[1][3], "4560");

        strcpy(tp[2][0], "sap0");
        strcpy(tp[2][1], "gop0");
        strcpy(tp[2][2], "zzz0");
        strcpy(tp[2][3], "7890");

        fun(&tp[1]);

        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        free( tp[i][j] );
                }
                free( tp[i] );
        }

        free(tp);

        return 0;
}
  • Step 5 : Output is as below

fun() : Access Strings and Print
gap0
pop0
yyy0
4560

fun() : Access Characters and Print
gap0
pop0
yyy0
4560

fun() : Access Strings and Print
gggg
gggg
gggg
gggg

fun() : Access Strings and Print
cccc
cccc
cccc
cccc
  • Step 1 : Consider a triple dimension array created using a character triple pointer

char ***tp;

tp = malloc( 3 * sizeof(char **) );

for (int i = 0; i < 3; i++)
{
        tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
        for (int j = 0; j < 4; j++)
        {
                tp[i][j] = malloc( 5 * sizeof(char) );
        }
}

strcpy(tp[0][0], "lap0");
strcpy(tp[0][1], "top0");
strcpy(tp[0][2], "xxx0");
strcpy(tp[0][3], "1230");

strcpy(tp[1][0], "gap0");
strcpy(tp[1][1], "pop0");
strcpy(tp[1][2], "yyy0");
strcpy(tp[1][3], "4560");

strcpy(tp[2][0], "sap0");
strcpy(tp[2][1], "gop0");
strcpy(tp[2][2], "zzz0");
strcpy(tp[2][3], "7890");
  • Step 2 : Pass Address of Triple Dimension array to a function

fun(&tp);
  • Step 3.1 : Define function fun

void fun(char ****ptr )
{

}
  • Step 3.2 : Access and Print the strings inside function fun

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 3.3 : Access and Print individual characters inside function fun

for(int i = 0; i < 3; i++)
{
        for(int j = 0; j < 4; j++)
        {
                for(int k = 0; k < 4; k++)
                {
                        printf("(*ptr)[%d][%d][%d] = %c\n", i, j, k, (*ptr)[i][j][k]);
                }
        }
}
  • Step 3.4 : Access and change strings inside function fun

for(int i = 0; i < 3; i++)
{
        for(int j = 0; j < 4; j++)
        {
                strcpy( (*ptr)[i][j], "ccc");
        }
}
  • Step 3.5 : Access and change individual characters inside function fun

for(int i = 0; i < 3; i++)
{
        for(int j = 0; j < 4; j++)
        {
                for(int k = 0; k < 5; k++)
                {
                        (*ptr)[i][j][k] = 'c';
                }
        }
}
  • Step 4 : Free memory after use

for (int i = 0; i < 3; i++)
{
        for (int j = 0; j < 4; j++)
        {
                free( tp[i][j] );
        }
        free( tp[i] );
}

free(tp);
  • See full program below

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void fun(char ****ptr)
{
        //Access and print individual 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]);
                }
        }

        //Access and print individual characters
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        for(int k = 0; k < 4; k++)
                        {
                                printf("(*ptr)[%d][%d][%d] = %c\n", i, j, k, (*ptr)[i][j][k]);
                        }
                }
        }

        //Access and change individual strings
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        strcpy( (*ptr)[i][j], "ccc");
                }
        }

        //Access and change individual characters
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        for(int k = 0; k < 5; k++)
                        {
                                (*ptr)[i][j][k] = 'c';
                        }
                }
        }
}

int main(void)
{
        char ***tp;

        tp = malloc( 3 * sizeof(char **) );

        for (int i = 0; i < 3; i++)
        {
                tp[i] = malloc( 4 * sizeof(sizeof(char *)) );
                for (int j = 0; j < 4; j++)
                {
                        tp[i][j] = malloc( 5 * sizeof(char) );
                }
        }

        strcpy(tp[0][0], "lap0");
        strcpy(tp[0][1], "top0");
        strcpy(tp[0][2], "xxx0");
        strcpy(tp[0][3], "1230");

        strcpy(tp[1][0], "gap0");
        strcpy(tp[1][1], "pop0");
        strcpy(tp[1][2], "yyy0");
        strcpy(tp[1][3], "4560");

        strcpy(tp[2][0], "sap0");
        strcpy(tp[2][1], "gop0");
        strcpy(tp[2][2], "zzz0");
        strcpy(tp[2][3], "7890");

        fun(&tp);

        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        free( tp[i][j] );
                }
                free( tp[i] );
        }

        free(tp);


        return 0;
}
  • Output is as below

(*ptr)[0][0] = lap0
(*ptr)[0][1] = top0
(*ptr)[0][2] = xxx0
(*ptr)[0][3] = 1230
(*ptr)[1][0] = gap0
(*ptr)[1][1] = pop0
(*ptr)[1][2] = yyy0
(*ptr)[1][3] = 4560
(*ptr)[2][0] = sap0
(*ptr)[2][1] = gop0
(*ptr)[2][2] = zzz0
(*ptr)[2][3] = 7890

(*ptr)[0][0][0] = l
(*ptr)[0][0][1] = a
(*ptr)[0][0][2] = p
(*ptr)[0][0][3] = 0
(*ptr)[0][1][0] = t
(*ptr)[0][1][1] = o
(*ptr)[0][1][2] = p
(*ptr)[0][1][3] = 0
(*ptr)[0][2][0] = x
(*ptr)[0][2][1] = x
(*ptr)[0][2][2] = x
(*ptr)[0][2][3] = 0
(*ptr)[0][3][0] = 1
(*ptr)[0][3][1] = 2
(*ptr)[0][3][2] = 3
(*ptr)[0][3][3] = 0

(*ptr)[1][0][0] = g
(*ptr)[1][0][1] = a
(*ptr)[1][0][2] = p
(*ptr)[1][0][3] = 0
(*ptr)[1][1][0] = p
(*ptr)[1][1][1] = o
(*ptr)[1][1][2] = p
(*ptr)[1][1][3] = 0
(*ptr)[1][2][0] = y
(*ptr)[1][2][1] = y
(*ptr)[1][2][2] = y
(*ptr)[1][2][3] = 0
(*ptr)[1][3][0] = 4
(*ptr)[1][3][1] = 5
(*ptr)[1][3][2] = 6
(*ptr)[1][3][3] = 0

(*ptr)[2][0][0] = s
(*ptr)[2][0][1] = a
(*ptr)[2][0][2] = p
(*ptr)[2][0][3] = 0
(*ptr)[2][1][0] = g
(*ptr)[2][1][1] = o
(*ptr)[2][1][2] = p
(*ptr)[2][1][3] = 0
(*ptr)[2][2][0] = z
(*ptr)[2][2][1] = z
(*ptr)[2][2][2] = z
(*ptr)[2][2][3] = 0
(*ptr)[2][3][0] = 7
(*ptr)[2][3][1] = 8
(*ptr)[2][3][2] = 9
(*ptr)[2][3][3] = 0