Functions and Integer Triple Pointer

In this section, you are going to learn

What are the calling conventions of integer triple pointer ?

Call by Value

Call by Reference

int ***tp;

Consider a integer triple pointer

int ***tp;

Let us answer few basic questions about integer 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(int x) {}

  • Call by Value

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

void fun(int x) {}

  • Call by Value

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

void fun(int x) {}

  • Call by Value

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

void fun(int *p) {}

  • Call by Reference

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

void fun(int *p) {}

  • Call by Reference

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

void fun(int *p) {}

  • Call by Reference

fun(tp[0][0])

void fun(int *p) {}

  • Call by Reference

fun(tp[1][0])

void fun(int *p) {}

  • Call by Reference

fun(tp[2][0])

void fun(int *p) {}

  • Call by Reference

fun(&tp[0][0])

void fun(int **p) {}

  • Call by Reference

fun(&tp[1][0])

void fun(int **p) {}

  • Call by Reference

fun(&tp[2][0])

void fun(int **p) {}

  • Call by Reference

fun(**tp)

void fun(int *p) {}

  • Call by Reference

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

void fun(int *p) {}

  • Call by Reference

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

void fun(int *p) {}

  • Call by Reference

fun(tp[0])

void fun(int **p) {}

  • Call by Reference

fun(tp[1])

void fun(int **p) {}

  • Call by Reference

fun(tp[2])

void fun(int **p) {}

  • Call by Reference

fun(&tp[0])

void fun(int ***p) {}

  • Call by Reference

fun(*tp)

void fun(int **p) {}

  • Call by Reference

fun(*(tp + 1))

void fun(int **p) {}

  • Call by Reference

fun(*(tp + 2))

void fun(int **p) {}

  • Call by Reference

fun(tp)

void fun(int ***p) {}

  • Call by Reference

fun(&tp)

void fun(int ****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 integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

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

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 integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

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

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 integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

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

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 integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

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

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 integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

for (int i = 0; i < 2; i++)
{
        for (int j = 0; j < 3; j++)
        {
                for (int k = 0; k < 4; k++)
                {
                        tp[i][j][k] = data++;
                }
        }
}
  • Step 2 : Pass tp[0][0][0], tp[1][0][0] to a function fun

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

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

void fun(int c)
{
        c = 999;
}
  • 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 integer inside function DOES NOT affect value of integer in Caller !

  • Step 5 : Free memory after use

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

free(tp);
  • See full program below

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

void fun(int c)
{
        c = 999;
}

int main(void)
{
        // Define a triple integer pointer
        int ***tp;

        // Allocate memory to create array[2][3][4]
        tp = malloc(2 * sizeof(int **) );

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

        // Assign data
        int data = 99;

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

        printf("----- Before Call By Value -----\n");

        printf("tp[0][0][0] = %d\n", tp[0][0][0]);
        printf("tp[1][0][0] = %d\n", tp[1][0][0]);

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

        printf("----- After Call By Value -----\n");

        printf("tp[0][0][0] = %d\n", tp[0][0][0]);
        printf("tp[1][0][0] = %d\n", tp[1][0][0]);

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

        free(tp);

        return 0;
}
  • Output is as below

----- Before Call By Value -----
tp[0][0][0] = 99
tp[1][0][0] = 111

----- After Call By Value -----
tp[0][0][0] = 99
tp[1][0][0] = 111
  • Step 1 : Consider a triple dimension array created using a integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

for (int i = 0; i < 2; i++)
{
        for (int j = 0; j < 3; j++)
        {
                for (int k = 0; k < 4; k++)
                {
                        tp[i][j][k] = data++;
                }
        }
}
  • Step 2 : Pass ***tp, *(*(*(tp + 1) + 0) + 0) to a function fun

fun( ***tp );

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

void fun(int c)
{
        c = 999;
}
  • 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 integer inside function DOES NOT affect value of integer in Caller !

  • Step 5 : Free memory after use

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

free(tp);
  • See full program below

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

void fun(int c)
{
        c = 999;
}

int main(void)
{
        // Define a triple integer pointer
        int ***tp;

        // Allocate memory to create array[2][3][4]
        tp = malloc(2 * sizeof(int **) );

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

        // Assign data
        int data = 99;

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

        printf("----- Before Call By Value -----\n");

        printf(" ***tp = %d\n", ***tp );
        printf(" *(*(*(tp + 1) + 0) + 0) = %d\n", *(*(*(tp + 1) + 0) + 0) );

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

        printf("----- After Call By Value -----\n");

        printf(" ***tp = %d\n", ***tp );
        printf(" *(*(*(tp + 1) + 0) + 0) = %d\n", *(*(*(tp + 1) + 0) + 0) );

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

        free(tp);

        return 0;
}
  • Output is as below

----- Before Call By Value -----
 ***tp = 99
 *(*(*(tp + 1) + 0) + 0) = 111

----- After Call By Value -----
 ***tp = 99
 *(*(*(tp + 1) + 0) + 0) = 111

Let us look at examples of Call by Reference

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

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

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

There are 6 single dimension arrays in int \*\*\*tp with respect to above allocation

tp[0][0]

tp[0][1]

tp[0][2]

tp[1][0]

tp[1][1]

tp[1][2]

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

fun( tp[0][0] );

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

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

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

fun( **tp );

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

void fun(int *ptr)
{
}
  • Step 4 : Note that it is call by Reference. Means contents of single dimension array can be changed inside function fun

void fun(int *ptr)
{
}
  • Step 5 : Free memory after use

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

free(tp);
  • See full program below

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

void fun(int *ptr)
{
        ptr[0] = 666;
        ptr[1] = 777;
        ptr[2] = 888;
        ptr[3] = 999;
}

int main(void)
{
        // Define a triple integer pointer
        int ***tp;

        // Allocate memory to create array[2][3][4]
        tp = malloc(2 * sizeof(int **) );

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

        // Assign data
        int data = 99;

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

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

        // Method 1 : Access Single dimension arrays

        fun( tp[0][0] );

        fun( tp[1][0] );

        // Method 2 : Access Single dimension arrays

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

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

        // Method 3 : Access Single dimension arrays

        fun( **tp );

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

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

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

        free(tp);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
tp[0][0][0] = 99 tp[0][0][1] = 100 tp[0][0][2] = 101 tp[0][0][3] = 102
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 111 tp[1][0][1] = 112 tp[1][0][2] = 113 tp[1][0][3] = 114
tp[1][1][0] = 115 tp[1][1][1] = 116 tp[1][1][2] = 117 tp[1][1][3] = 118
tp[1][2][0] = 119 tp[1][2][1] = 120 tp[1][2][2] = 121 tp[1][2][3] = 122

----- After Call By Reference -----
tp[0][0][0] = 666 tp[0][0][1] = 777 tp[0][0][2] = 888 tp[0][0][3] = 999
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 666 tp[1][0][1] = 777 tp[1][0][2] = 888 tp[1][0][3] = 999
tp[1][1][0] = 115 tp[1][1][1] = 116 tp[1][1][2] = 117 tp[1][1][3] = 118
tp[1][2][0] = 119 tp[1][2][1] = 120 tp[1][2][2] = 121 tp[1][2][3] = 122
  • Step 1 : Consider a triple dimension array created using a integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

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

There are 6 single dimension arrays in

tp[0][0]

tp[0][1]

tp[0][2]

tp[1][0]

tp[1][1]

tp[1][2]

Address of single dimension arrays is simply

&tp[0][0]

&tp[0][1]

&tp[0][2]

&tp[1][0]

&tp[1][1]

&tp[1][2]

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

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

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

fun( tp[0] );

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

fun( *tp );

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

void fun(int **ptr )
{

}
  • Step 3.3 : Define the function fun to change the contents of single dimension array integer by integer

void fun(int **ptr)
{
        (*ptr)[0] = 666;
        (*ptr)[1] = 777;
        (*ptr)[2] = 888;
        (*ptr)[3] = 999;
}
  • Step 4 : Free memory after use

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

free(tp);
  • See full program below

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

void fun(int **ptr )
{
        (*ptr)[0] = 666;
        (*ptr)[1] = 777;
        (*ptr)[2] = 888;
        (*ptr)[3] = 999;
}

int main(void)
{
        // Define a triple integer pointer
        int ***tp;

        // Allocate memory to create array[2][3][4]
        tp = malloc(2 * sizeof(int **) );

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

        // Assign data
        int data = 99;

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

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

        // Method 1 : Access Single dimension arrays

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

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

        // Method 2 : Access Single dimension arrays

        fun( tp[0] );

        fun( tp[1] );

        // Method 3 : Access Single dimension arrays

        fun( *tp );

        fun( *(tp + 1) );

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

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

        free(tp);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
tp[0][0][0] = 99 tp[0][0][1] = 100 tp[0][0][2] = 101 tp[0][0][3] = 102
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 111 tp[1][0][1] = 112 tp[1][0][2] = 113 tp[1][0][3] = 114
tp[1][1][0] = 115 tp[1][1][1] = 116 tp[1][1][2] = 117 tp[1][1][3] = 118
tp[1][2][0] = 119 tp[1][2][1] = 120 tp[1][2][2] = 121 tp[1][2][3] = 122

----- After Call By Reference -----
tp[0][0][0] = 666 tp[0][0][1] = 777 tp[0][0][2] = 888 tp[0][0][3] = 999
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 666 tp[1][0][1] = 777 tp[1][0][2] = 888 tp[1][0][3] = 999
tp[1][1][0] = 115 tp[1][1][1] = 116 tp[1][1][2] = 117 tp[1][1][3] = 118
tp[1][2][0] = 119 tp[1][2][1] = 120 tp[1][2][2] = 121 tp[1][2][3] = 122
  • Step 1 : Consider a triple dimension array created using a integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

for (int i = 0; i < 2; i++)
{
        for (int j = 0; j < 3; j++)
        {
                for (int k = 0; k < 4; k++)
                {
                        tp[i][j][k] = data++;
                }
        }
}
  • Step 2 : Pass Double dimension array to function fun

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

void fun(int **ptr)
{
        // Access Integers and Change
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        ptr[i][j] = 999;
                }
        }
}
  • Step 3.2 : Access and Change integers inside function fun


  • Step 4 : Free memory after use

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

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

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

void fun(int **ptr)
{
        // Access Integers and Change
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        ptr[i][j] = 999;
                }
        }
}

int main(void)
{
        // Define a triple integer pointer
        int ***tp;

        // Allocate memory to create array[2][3][4]
        tp = malloc(2 * sizeof(int **) );

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

        // Assign data
        int data = 99;

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

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

        fun(tp[1]);

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

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

        free(tp);

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

----- Before Call By Reference -----
tp[0][0][0] = 99 tp[0][0][1] = 100 tp[0][0][2] = 101 tp[0][0][3] = 102
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 111 tp[1][0][1] = 112 tp[1][0][2] = 113 tp[1][0][3] = 114
tp[1][1][0] = 115 tp[1][1][1] = 116 tp[1][1][2] = 117 tp[1][1][3] = 118
tp[1][2][0] = 119 tp[1][2][1] = 120 tp[1][2][2] = 121 tp[1][2][3] = 122

----- After Call By Reference -----
tp[0][0][0] = 99 tp[0][0][1] = 100 tp[0][0][2] = 101 tp[0][0][3] = 102
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 999 tp[1][0][1] = 999 tp[1][0][2] = 999 tp[1][0][3] = 999
tp[1][1][0] = 999 tp[1][1][1] = 999 tp[1][1][2] = 999 tp[1][1][3] = 999
tp[1][2][0] = 999 tp[1][2][1] = 999 tp[1][2][2] = 999 tp[1][2][3] = 999
  • Step 1 : Consider a triple dimension array created using a integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

for (int i = 0; i < 2; i++)
{
        for (int j = 0; j < 3; j++)
        {
                for (int k = 0; k < 4; k++)
                {
                        tp[i][j][k] = data++;
                }
        }
}
  • Step 2 : Pass Address of Double dimension array to function fun

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

void fun(int ***ptr)
{

}
  • Step 3.2 : Access and Change integers inside function fun

void fun(int ***ptr)
{
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        (*ptr)[i][j] = 999;
                }
        }
}
  • Step 4 : Free memory after use

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

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

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

void fun(int ***ptr)
{
        // Access Integers and Change
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        (*ptr)[i][j] = 999;
                }
        }
}

int main(void)
{
        // Define a triple integer pointer
        int ***tp;

        // Allocate memory to create array[2][3][4]
        tp = malloc(2 * sizeof(int **) );

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

        // Assign data
        int data = 99;

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

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

        fun(&tp[1]);

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

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

        free(tp);

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

----- Before Call By Reference -----
tp[0][0][0] = 99 tp[0][0][1] = 100 tp[0][0][2] = 101 tp[0][0][3] = 102
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 111 tp[1][0][1] = 112 tp[1][0][2] = 113 tp[1][0][3] = 114
tp[1][1][0] = 115 tp[1][1][1] = 116 tp[1][1][2] = 117 tp[1][1][3] = 118
tp[1][2][0] = 119 tp[1][2][1] = 120 tp[1][2][2] = 121 tp[1][2][3] = 122

----- After Call By Reference -----
tp[0][0][0] = 99 tp[0][0][1] = 100 tp[0][0][2] = 101 tp[0][0][3] = 102
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 999 tp[1][0][1] = 999 tp[1][0][2] = 999 tp[1][0][3] = 999
tp[1][1][0] = 999 tp[1][1][1] = 999 tp[1][1][2] = 999 tp[1][1][3] = 999
tp[1][2][0] = 999 tp[1][2][1] = 999 tp[1][2][2] = 999 tp[1][2][3] = 999
  • Step 1 : Consider a triple dimension array created using a integer triple pointer

// Define a triple integer pointer
int ***tp;

// Allocate memory to create array[2][3][4]
tp = malloc(2 * sizeof(int **) );

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

// Assign data
int data = 99;

for (int i = 0; i < 2; i++)
{
        for (int j = 0; j < 3; j++)
        {
                for (int k = 0; k < 4; k++)
                {
                        tp[i][j][k] = data++;
                }
        }
}
  • Step 2 : Pass Address of Triple Dimension array to a function

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

void fun(int ****ptr )
{

}
  • Step 3.2 : Access and change individual integers inside function fun

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

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

free(tp);
  • See full program below

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

void fun(int ****ptr)
{
        //Access and change individual integers
        for(int i = 0; i < 2; i++)
        {
                for(int j = 0; j < 3; j++)
                {
                        for(int k = 0; k < 4; k++)
                        {
                                (*ptr)[i][j][k] = 999;
                        }
                }
        }
}

int main(void)
{
        // Define a triple integer pointer
        int ***tp;

        // Allocate memory to create array[2][3][4]
        tp = malloc(2 * sizeof(int **) );

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

        // Assign data
        int data = 99;

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

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

        fun(&tp);

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 2; i++)
        {
                for (int j = 0; j < 3; j++)
                {
                        for (int k = 0; k < 4; k++)
                        {
                                printf("tp[%d][%d][%d] = %d ", i, j, k, tp[i][j][k]);
                        }
                        printf("\n");
                }
                printf("\n");
        }

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

        free(tp);

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
tp[0][0][0] = 99 tp[0][0][1] = 100 tp[0][0][2] = 101 tp[0][0][3] = 102
tp[0][1][0] = 103 tp[0][1][1] = 104 tp[0][1][2] = 105 tp[0][1][3] = 106
tp[0][2][0] = 107 tp[0][2][1] = 108 tp[0][2][2] = 109 tp[0][2][3] = 110

tp[1][0][0] = 111 tp[1][0][1] = 112 tp[1][0][2] = 113 tp[1][0][3] = 114
tp[1][1][0] = 115 tp[1][1][1] = 116 tp[1][1][2] = 117 tp[1][1][3] = 118
tp[1][2][0] = 119 tp[1][2][1] = 120 tp[1][2][2] = 121 tp[1][2][3] = 122

----- After Call By Reference -----
tp[0][0][0] = 999 tp[0][0][1] = 999 tp[0][0][2] = 999 tp[0][0][3] = 999
tp[0][1][0] = 999 tp[0][1][1] = 999 tp[0][1][2] = 999 tp[0][1][3] = 999
tp[0][2][0] = 999 tp[0][2][1] = 999 tp[0][2][2] = 999 tp[0][2][3] = 999

tp[1][0][0] = 999 tp[1][0][1] = 999 tp[1][0][2] = 999 tp[1][0][3] = 999
tp[1][1][0] = 999 tp[1][1][1] = 999 tp[1][1][2] = 999 tp[1][1][3] = 999
tp[1][2][0] = 999 tp[1][2][1] = 999 tp[1][2][2] = 999 tp[1][2][3] = 999