Functions and Integer Triple Dimension Array

In this section, you are going to learn

What are the calling conventions of integer triple dimension array ?

Call by Value

Call by Reference

int array_name[Block][Row][Column];

Consider a integer triple dimension array

int a[2][3][4];

Let us answer few basic questions in this array

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

Function Call

Function Definition

Observations

fun(a[0][0][0])

void fun(int x) {}

  • Call by Value

fun(a[1][0][0])

void fun(int x) {}

  • Call by Value

fun(a[2][0][0])

void fun(int x) {}

  • Call by Value

fun(&a[0][0][0])

void fun(int *p) {}

  • Call by Reference

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

void fun(int *p) {}

  • Call by Reference

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

void fun(int *p) {}

  • Call by Reference

fun(a[0][0])

void fun(int *p) {}

  • Call by Reference

fun(a[1][0])

void fun(int *p) {}

  • Call by Reference

fun(a[2][0])

void fun(int *p) {}

  • Call by Reference

fun(&a[0][0])

void fun(int (*p)[5]) {}

  • Call by Reference

fun(&a[1][0])

void fun(int (*p)[5]) {}

  • Call by Reference

fun(&a[2][0])

void fun(int (*p)[5]) {}

  • Call by Reference

fun(**a)

void fun(int *p) {}

  • Call by Reference

fun(*(*(a + 1) + 0))

void fun(int *p) {}

  • Call by Reference

fun(*(*(a + 2) + 0))

void fun(int *p) {}

  • Call by Reference

fun(a[0])

void fun(int (*p)[5]) {}

  • Call by Reference

fun(a[1])

void fun(int (*p)[5]) {}

  • Call by Reference

fun(a[2])

void fun(int (*p)[5]) {}

  • Call by Reference

fun(&a[0])

void fun(int (*p)[4][5]) {}

  • Call by Reference

fun(&a[1])

void fun(int (*p)[4][5]) {}

  • Call by Reference

fun(&a[2])

void fun(int (*p)[4][5]) {}

  • Call by Reference

fun(*a)

void fun(int (*p)[5]) {}

  • Call by Reference

fun(*(a + 1))

void fun(int (*p)[5]) {}

  • Call by Reference

fun(*(a + 2))

void fun(int (*p)[5]) {}

  • Call by Reference

fun(a)

void fun(int (*p)[4][5]) {}

  • Call by Reference

fun(a + 1)

void fun(int (*p)[4][5]) {}

  • Call by Reference

fun(a + 2)

void fun(int (*p)[4][5]) {}

  • Call by Reference

fun(&a)

void fun(int (*p)[3][4][5]) {}

  • 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 an array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};

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

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

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

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

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

  • Step 1 : Consider an array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};

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

  • Step 2 : Consider an expression ***a

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

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence ***a 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 : &a[0][0][0], &a[1][2][3]

If Declaration has THREE dereference operators, and

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

  • then it is call by reference

  • Example : a[0][0]

If Declaration has THREE dereference operators, and

  • Expression has ONE dereference operators, * OR [ ]

  • then it is call by reference

  • Example : a[0]

If Declaration has THREE dereference operators, and

  • Expression has ZERO dereference operators

  • then it is call by reference

  • Example : a

  • Step 1 : Consider an array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};

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

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

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

Note : [ ] and * are dereference operators

Condition 3 : Expression has & operator

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

  • Step 1 : Consider an array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};

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

  • Step 2 : Consider an expression a[1]

Condition 2 : Expression has ONE dereference operator

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence a[1] is Call By Reference

Let us look at examples of Call by Value

  • Step 1 : Consider a THREE dimensional array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};
  • Step 2 : Pass a[0][0][0], a[1][0][0], a[2][0][0] to a function fun

fun(a[0][0][0]);

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

void fun(int c)
{
        c = 99;
}
  • 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 !

  • See full program below

#include <stdio.h>

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

int main(void)
{
        int a[2][3][4] = {
                {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                },
                {
                        {13, 14, 15, 16},
                        {17, 18, 19, 20},
                        {21, 22, 23, 24},
                }
        };

        printf("----- Before Call by Value -----\n");
        printf("a[0][0][0] = %d\n", a[0][0][0]);
        printf("a[1][0][0] = %d\n", a[1][0][0]);

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

        printf("----- After Call by Value -----\n");
        printf("a[0][0][0] = %d\n", a[0][0][0]);
        printf("a[1][0][0] = %d\n", a[1][0][0]);

        return 0;
}
  • Output is as below

----- Before Call by Value -----
a[0][0][0] = 1
a[1][0][0] = 13

----- After Call by Value -----
a[0][0][0] = 1
a[1][0][0] = 13
  • Step 1 : Consider a two dimensional array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};
  • Step 2 : Pass ***a, *(*(*(a + 1) + 0) + 0), *(*(*(a + 2) + 0) + 0) to a function fun

fun( ***a );

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

void fun(int c)
{
        c = 99;
}
  • 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 !

  • See full program below

#include <stdio.h>

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

int main(void)
{
        int a[2][3][4] = {
                {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                },
                {
                        {13, 14, 15, 16},
                        {17, 18, 19, 20},
                        {21, 22, 23, 24},
                }
        };

        printf("----- Before call by value -----\n");

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

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

        printf("----- After call by value -----\n");

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

        return 0;
}
  • Output is as below

----- Before call by value -----
***a = 1
*(*(*(a + 1) + 0) + 0) = 13

----- After call by value -----
***a = 1
*(*(*(a + 1) + 0) + 0) = 13

Let us look at examples of Call by Reference

  • Step 1 : Consider a two dimensional array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};

There are 6 single dimension arrays in int a[2][3][4]

a[0][0]

a[0][1]

a[0][2]

a[1][0]

a[1][1]

a[1][2]

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

fun( a[0][0] );

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

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

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

fun( **a );

fun( *(*(a + 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)
{
        ptr[0] = 66;
        ptr[1] = 77;
        ptr[2] = 88;
        ptr[3] = 99;
}
  • See full program below

#include <stdio.h>

void fun(int *ptr)
{
        ptr[0] = 66;
        ptr[1] = 77;
        ptr[2] = 88;
        ptr[3] = 99;
}

int main(void)
{
        int a[2][3][4] = {
                {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                },
                {
                        {13, 14, 15, 16},
                        {17, 18, 19, 20},
                        {21, 22, 23, 24},
                }
        };

        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("a[%d][%d][%d] = %d\n", i, j, k, a[i][j][k]);
                        }
                }
        }

        //Method 1 : Access Single dimension arrays

        fun( a[0][0] );

        fun( a[1][0] );

        //Method 2 : Access Single dimension arrays

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

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

        //Method 3 : Access Single dimension arrays

        fun( **a );

        fun( *(*(a + 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("a[%d][%d][%d] = %d\n", i, j, k, a[i][j][k]);
                        }
                }
        }

        return 0;
}
  • Output is as below

----- Before call by reference -----
a[0][0][0] = 1
a[0][0][1] = 2
a[0][0][2] = 3
a[0][0][3] = 4
a[0][1][0] = 5
a[0][1][1] = 6
a[0][1][2] = 7
a[0][1][3] = 8
a[0][2][0] = 9
a[0][2][1] = 10
a[0][2][2] = 11
a[0][2][3] = 12
a[1][0][0] = 13
a[1][0][1] = 14
a[1][0][2] = 15
a[1][0][3] = 16
a[1][1][0] = 17
a[1][1][1] = 18
a[1][1][2] = 19
a[1][1][3] = 20
a[1][2][0] = 21
a[1][2][1] = 22
a[1][2][2] = 23
a[1][2][3] = 24

----- After call by reference -----
a[0][0][0] = 66
a[0][0][1] = 77
a[0][0][2] = 88
a[0][0][3] = 99
a[0][1][0] = 5
a[0][1][1] = 6
a[0][1][2] = 7
a[0][1][3] = 8
a[0][2][0] = 9
a[0][2][1] = 10
a[0][2][2] = 11
a[0][2][3] = 12
a[1][0][0] = 66
a[1][0][1] = 77
a[1][0][2] = 88
a[1][0][3] = 99
a[1][1][0] = 17
a[1][1][1] = 18
a[1][1][2] = 19
a[1][1][3] = 20
a[1][2][0] = 21
a[1][2][1] = 22
a[1][2][2] = 23
a[1][2][3] = 24
  • Step 1 : Consider a THREE dimensional array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};

There are 6 single dimension arrays in int a[2][3][4]

a[0][0]

a[0][1]

a[0][2]

a[1][0]

a[1][1]

a[1][2]

Address of single dimension arrays is simply

&a[0][0]

&a[0][1]

&a[0][2]

&a[1][0]

&a[1][1]

&a[1][2]

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

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

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

fun( a[0] );

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

fun( *a );

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

void fun(int (*ptr)[4] )
{
}
  • Step 3.2 : Define the function fun to change the contents of single dimension array integer by integer

void fun(int (*ptr)[4] )
{
        (*ptr)[0] = 66;
        (*ptr)[1] = 77;
        (*ptr)[2] = 88;
        (*ptr)[3] = 99;
}
  • See full program below

#include <stdio.h>

void fun(int (*ptr)[4] )
{
        (*ptr)[0] = 66;
        (*ptr)[1] = 77;
        (*ptr)[2] = 88;
        (*ptr)[3] = 99;
}

int main(void)
{
        int a[2][3][4] = {
                {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                },
                {
                        {13, 14, 15, 16},
                        {17, 18, 19, 20},
                        {21, 22, 23, 24},
                }
        };

        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("a[%d][%d][%d] = %d\n", i, j, k, a[i][j][k]);
                        }
                }
        }

        //Method 1 : Access Single dimension arrays

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

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


        //Method 2 : Access Single dimension arrays

        fun( a[0] );

        fun( a[1] );

        //Method 3 : Access Single dimension arrays

        fun( *a );

        fun( *(a + 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("a[%d][%d][%d] = %d\n", i, j, k, a[i][j][k]);
                        }
                }
        }

        return 0;
}
  • Output is as below

----- Before call by reference -----
a[0][0][0] = 1
a[0][0][1] = 2
a[0][0][2] = 3
a[0][0][3] = 4
a[0][1][0] = 5
a[0][1][1] = 6
a[0][1][2] = 7
a[0][1][3] = 8
a[0][2][0] = 9
a[0][2][1] = 10
a[0][2][2] = 11
a[0][2][3] = 12
a[1][0][0] = 13
a[1][0][1] = 14
a[1][0][2] = 15
a[1][0][3] = 16
a[1][1][0] = 17
a[1][1][1] = 18
a[1][1][2] = 19
a[1][1][3] = 20
a[1][2][0] = 21
a[1][2][1] = 22
a[1][2][2] = 23
a[1][2][3] = 24

----- After call by reference -----
a[0][0][0] = 66
a[0][0][1] = 77
a[0][0][2] = 88
a[0][0][3] = 99
a[0][1][0] = 5
a[0][1][1] = 6
a[0][1][2] = 7
a[0][1][3] = 8
a[0][2][0] = 9
a[0][2][1] = 10
a[0][2][2] = 11
a[0][2][3] = 12
a[1][0][0] = 66
a[1][0][1] = 77
a[1][0][2] = 88
a[1][0][3] = 99
a[1][1][0] = 17
a[1][1][1] = 18
a[1][1][2] = 19
a[1][1][3] = 20
a[1][2][0] = 21
a[1][2][1] = 22
a[1][2][2] = 23
a[1][2][3] = 24
  • Step 1 : Consider a two dimensional array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};
  • Step 2 : Pass Double dimension array to function fun

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

void fun(int (*ptr)[4])
{

}
  • Step 3.3 : Access and Print integers inside function fun

void fun(int (*ptr)[4])
{
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        printf("ptr[%d][%d] = %d\n", i, j, ptr[i][j] );
                }
                printf("\n");
        }
}
  • Step 3.5 : Access and Change integers inside function fun

void fun(int (*ptr)[4])
{
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        ptr[i][j] = 99;
                }
        }
}
  • Step 4 : See the full program below

#include <stdio.h>

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


}

int main(void)
{
        int a[2][3][4] = {
                {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                },
                {
                        {13, 14, 15, 16},
                        {17, 18, 19, 20},
                        {21, 22, 23, 24},
                }
        };

        printf("----- Before call by reference -----\n");
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        printf("a[1][%d][%d] = %d\n", i, j, a[1][i][j] );
                }
                printf("\n");
        }

        fun(a[1]);

        printf("----- After call by reference -----\n");
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        printf("a[1][%d][%d] = %d\n", i, j, a[1][i][j] );
                }
                printf("\n");
        }

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

----- Before call by reference -----
a[1][0][0] = 13
a[1][0][1] = 14
a[1][0][2] = 15
a[1][0][3] = 16

a[1][1][0] = 17
a[1][1][1] = 18
a[1][1][2] = 19
a[1][1][3] = 20

a[1][2][0] = 21
a[1][2][1] = 22
a[1][2][2] = 23
a[1][2][3] = 24

----- After call by reference -----
a[1][0][0] = 99
a[1][0][1] = 99
a[1][0][2] = 99
a[1][0][3] = 99

a[1][1][0] = 99
a[1][1][1] = 99
a[1][1][2] = 99
a[1][1][3] = 99

a[1][2][0] = 99
a[1][2][1] = 99
a[1][2][2] = 99
a[1][2][3] = 99
  • Step 1 : Consider a two dimensional array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};
  • Step 2 : Pass Address of Double dimension array to function fun

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

void fun(int (*ptr)[3][4])
{

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

void fun(int (*ptr)[3][4])
{
        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        printf("%d ", (*ptr)[i][j]);
                }
                printf("\n");
        }
}
  • Step 3.5 : Access and Change integers inside function fun

void fun(int (*ptr)[3][4])
{
        int data = 99;

        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        (*ptr)[i][j] = data++;
                }
        }
}
  • Step 4 : See the full program below

#include <stdio.h>

void fun(int (*ptr)[3][4])
{
        int data = 99;

        for(int i = 0; i < 3; i++)
        {
                for(int j = 0; j < 4; j++)
                {
                        (*ptr)[i][j] = data++;
                }
        }
}

int main(void)
{
        int a[2][3][4] = {
                {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                },
                {
                        {13, 14, 15, 16},
                        {17, 18, 19, 20},
                        {21, 22, 23, 24},
                }
        };

        printf("----- Before call by reference -----\n");

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

        fun(&a[1]);

        printf("----- After call by reference -----\n");

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

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

----- Before call by reference -----
a[1][0][0] = 13
a[1][0][1] = 14
a[1][0][2] = 15
a[1][0][3] = 16

a[1][1][0] = 17
a[1][1][1] = 18
a[1][1][2] = 19
a[1][1][3] = 20

a[1][2][0] = 21
a[1][2][1] = 22
a[1][2][2] = 23
a[1][2][3] = 24

----- After call by reference -----
a[1][0][0] = 99
a[1][0][1] = 100
a[1][0][2] = 101
a[1][0][3] = 102

a[1][1][0] = 103
a[1][1][1] = 104
a[1][1][2] = 105
a[1][1][3] = 106

a[1][2][0] = 107
a[1][2][1] = 108
a[1][2][2] = 109
a[1][2][3] = 110
  • Step 1 : Consider a two dimensional array

int a[2][3][4] = {
        {
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
        },
        {
                {13, 14, 15, 16},
                {17, 18, 19, 20},
                {21, 22, 23, 24},
        }
};
  • Step 2 : Pass Address of Triple Dimension array to a function

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

void fun(int (*ptr)[2][3][4] )
{

}
  • Step 3.2 : Access and Print 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++)
                {
                        printf("(*ptr)[%d][%d][%d] = %d\n", i, j, k, (*ptr)[i][j][k]);
                }
        }
}
  • Step 3.3 : Access and change individual integers inside function fun

int data = 99;

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] = data++;
                }
        }
}
  • See full program below

#include <stdio.h>

void fun(int (*ptr)[2][3][4])
{
        int data = 99;

        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] = data++;
                        }
                }
        }
}

int main(void)
{
        int a[2][3][4] = {
                {
                        {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                },
                {
                        {13, 14, 15, 16},
                        {17, 18, 19, 20},
                        {21, 22, 23, 24},
                }
        };

        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("a[%d][%d][%d] = %d\n", i, j, k, a[i][j][k]);
                        }
                }
        }

        fun(&a);

        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("a[%d][%d][%d] = %d\n", i, j, k, a[i][j][k]);
                        }
                }
        }

        return 0;
}
  • Output is as below

----- Before call by reference -----
a[0][0][0] = 1
a[0][0][1] = 2
a[0][0][2] = 3
a[0][0][3] = 4
a[0][1][0] = 5
a[0][1][1] = 6
a[0][1][2] = 7
a[0][1][3] = 8
a[0][2][0] = 9
a[0][2][1] = 10
a[0][2][2] = 11
a[0][2][3] = 12
a[1][0][0] = 13
a[1][0][1] = 14
a[1][0][2] = 15
a[1][0][3] = 16
a[1][1][0] = 17
a[1][1][1] = 18
a[1][1][2] = 19
a[1][1][3] = 20
a[1][2][0] = 21
a[1][2][1] = 22
a[1][2][2] = 23
a[1][2][3] = 24

----- After call by reference -----
a[0][0][0] = 99
a[0][0][1] = 100
a[0][0][2] = 101
a[0][0][3] = 102
a[0][1][0] = 103
a[0][1][1] = 104
a[0][1][2] = 105
a[0][1][3] = 106
a[0][2][0] = 107
a[0][2][1] = 108
a[0][2][2] = 109
a[0][2][3] = 110
a[1][0][0] = 111
a[1][0][1] = 112
a[1][0][2] = 113
a[1][0][3] = 114
a[1][1][0] = 115
a[1][1][1] = 116
a[1][1][2] = 117
a[1][1][3] = 118
a[1][2][0] = 119
a[1][2][1] = 120
a[1][2][2] = 121
a[1][2][3] = 122