Functions and Structure Triple Dimension Array

In this section, you are going to learn

What are the calling conventions of structure triple dimension array ?

Call by Value

Call by Reference

struct ABC {
        type1 member1;
        type2 member2;
        type3 member3;
        etc.,
};

struct ABC array_name[Block][Row][Column];

Consider a structure triple dimension array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[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(x[0][0][0])

void fun(struct ABC x) {}

  • Call by Value

fun(x[1][0][0])

void fun(struct ABC x) {}

  • Call by Value

fun(&x[0][0][0])

void fun(struct ABC *p) {}

  • Call by Reference

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

void fun(struct ABC *p) {}

  • Call by Reference

fun(x[0][0])

void fun(struct ABC *p) {}

  • Call by Reference

fun(x[1][0])

void fun(struct ABC *p) {}

  • Call by Reference

fun(&x[0][0])

void fun(struct ABC (*p)[4]) {}

  • Call by Reference

fun(&x[1][0])

void fun(struct ABC (*p)[4]) {}

  • Call by Reference

fun(**x)

void fun(struct ABC *p) {}

  • Call by Reference

fun(*(*(x + 1) + 0))

void fun(struct ABC *p) {}

  • Call by Reference

fun(x[0])

void fun(struct ABC (*p)[4]) {}

  • Call by Reference

fun(x[1])

void fun(struct ABC (*p)[4]) {}

  • Call by Reference

fun(&x[0])

void fun(struct ABC (*p)[3][4]) {}

  • Call by Reference

fun(&x[1])

void fun(struct ABC (*p)[3][4]) {}

  • Call by Reference

fun(*x)

void fun(struct ABC (*p)[4]) {}

  • Call by Reference

fun(*(x + 1))

void fun(struct ABC (*p)[4]) {}

  • Call by Reference

fun(x)

void fun(struct ABC (*p)[3][4]) {}

  • Call by Reference

fun(x + 1)

void fun(struct ABC (*p)[3][4]) {}

  • Call by Reference

fun(&x)

void fun(struct ABC (*p)[2][3][4]) {}

  • 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

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];

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

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

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

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

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

  • Step 1 : Consider an array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];

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

  • Step 2 : Consider an expression ***x

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

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

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

If Declaration has THREE dereference operators, and

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

  • then it is call by reference

  • Example : x[0][0]

If Declaration has THREE dereference operators, and

  • Expression has ONE dereference operators, * OR [ ]

  • then it is call by reference

  • Example : x[0]

If Declaration has THREE dereference operators, and

  • Expression has ZERO dereference operators

  • then it is call by reference

  • Example : x

  • Step 1 : Consider an array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];

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

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

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

Note : [ ] and * are dereference operators

Condition 3 : Expression has & operator

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

  • Step 1 : Consider an array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];

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

  • Step 2 : Consider an expression x[1]

Condition 2 : Expression has ONE dereference operator

Note : [ ] and * are dereference operators

Condition 3 : Expression DOES NOT have & operator

Hence x[1] is Call By Reference

Let us look at examples of Call by Value

  • Step 1 : Consider a THREE dimensional array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4] = {
        {
                {
                        {.a = 1, .b = 2, .c = 3},
                        {.a = 4, .b = 5, .c = 6},
                        {.a = 7, .b = 8, .c = 9},
                        {.a = 10, .b = 11, .c = 12},
                },
                {
                        {.a = 13, .b = 14, .c = 15},
                        {.a = 16, .b = 17, .c = 18},
                        {.a = 19, .b = 20, .c = 21},
                        {.a = 22, .b = 23, .c = 24},
                },
                {
                        {.a = 25, .b = 26, .c = 27},
                        {.a = 28, .b = 29, .c = 30},
                        {.a = 31, .b = 32, .c = 33},
                        {.a = 34, .b = 35, .c = 36},
                },
        },
        {
                {
                        {.a = 37, .b = 38, .c = 39},
                        {.a = 40, .b = 41, .c = 42},
                        {.a = 43, .b = 44, .c = 45},
                        {.a = 46, .b = 47, .c = 48},
                },
                {
                        {.a = 49, .b = 50, .c = 51},
                        {.a = 52, .b = 53, .c = 54},
                        {.a = 55, .b = 56, .c = 57},
                        {.a = 58, .b = 59, .c = 60},
                },
                {
                        {.a = 61, .b = 62, .c = 63},
                        {.a = 64, .b = 65, .c = 66},
                        {.a = 67, .b = 68, .c = 69},
                        {.a = 70, .b = 71, .c = 72},
                },
        },
};
  • Step 2 : Pass x[0][0][0], x[1][0][0] to a function fun

fun(x[0][0][0]);

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

void fun(struct ABC y)
{
        y.a = 777;
        y.b = 888;
        y.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 structure inside function DOES NOT affect value of structure in Caller !

  • See full program below

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC y)
{
        y.a = 777;
        y.b = 888;
        y.c = 999;
}

int main(void)
{
        struct ABC x[2][3][4] = {
                {
                        {
                                {.a = 1, .b = 2, .c = 3},
                                {.a = 4, .b = 5, .c = 6},
                                {.a = 7, .b = 8, .c = 9},
                                {.a = 10, .b = 11, .c = 12},
                        },
                        {
                                {.a = 13, .b = 14, .c = 15},
                                {.a = 16, .b = 17, .c = 18},
                                {.a = 19, .b = 20, .c = 21},
                                {.a = 22, .b = 23, .c = 24},
                        },
                        {
                                {.a = 25, .b = 26, .c = 27},
                                {.a = 28, .b = 29, .c = 30},
                                {.a = 31, .b = 32, .c = 33},
                                {.a = 34, .b = 35, .c = 36},
                        },
                },
                {
                        {
                                {.a = 37, .b = 38, .c = 39},
                                {.a = 40, .b = 41, .c = 42},
                                {.a = 43, .b = 44, .c = 45},
                                {.a = 46, .b = 47, .c = 48},
                        },
                        {
                                {.a = 49, .b = 50, .c = 51},
                                {.a = 52, .b = 53, .c = 54},
                                {.a = 55, .b = 56, .c = 57},
                                {.a = 58, .b = 59, .c = 60},
                        },
                        {
                                {.a = 61, .b = 62, .c = 63},
                                {.a = 64, .b = 65, .c = 66},
                                {.a = 67, .b = 68, .c = 69},
                                {.a = 70, .b = 71, .c = 72},
                        },
                },
        };

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

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

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

        return 0;
}
  • Output is as below

----- Before Call By Value -----
x[0][0][0].a = 1
x[1][0][0].a = 37

----- After Call By Value -----
x[0][0][0].a = 1
x[1][0][0].a = 37
  • Step 1 : Consider a THREE dimensional array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];
  • Step 2 : Pass ***x, *(*(*(x + 1) + 0) + 0) to a function fun

fun( ***x );

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

void fun(struct ABC y)
{
        y.a = 777;
        y.b = 888;
        y.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 structure inside function DOES NOT affect value of structure in Caller !

  • See full program below

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC y)
{
        y.a = 777;
        y.b = 888;
        y.c = 999;
}

int main(void)
{
        struct ABC x[2][3][4] = {
                {
                        {
                                {.a = 1, .b = 2, .c = 3},
                                {.a = 4, .b = 5, .c = 6},
                                {.a = 7, .b = 8, .c = 9},
                                {.a = 10, .b = 11, .c = 12},
                        },
                        {
                                {.a = 13, .b = 14, .c = 15},
                                {.a = 16, .b = 17, .c = 18},
                                {.a = 19, .b = 20, .c = 21},
                                {.a = 22, .b = 23, .c = 24},
                        },
                        {
                                {.a = 25, .b = 26, .c = 27},
                                {.a = 28, .b = 29, .c = 30},
                                {.a = 31, .b = 32, .c = 33},
                                {.a = 34, .b = 35, .c = 36},
                        },
                },
                {
                        {
                                {.a = 37, .b = 38, .c = 39},
                                {.a = 40, .b = 41, .c = 42},
                                {.a = 43, .b = 44, .c = 45},
                                {.a = 46, .b = 47, .c = 48},
                        },
                        {
                                {.a = 49, .b = 50, .c = 51},
                                {.a = 52, .b = 53, .c = 54},
                                {.a = 55, .b = 56, .c = 57},
                                {.a = 58, .b = 59, .c = 60},
                        },
                        {
                                {.a = 61, .b = 62, .c = 63},
                                {.a = 64, .b = 65, .c = 66},
                                {.a = 67, .b = 68, .c = 69},
                                {.a = 70, .b = 71, .c = 72},
                        },
                },
        };

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

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

        fun( ***x );

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

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

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

        return 0;
}
  • Output is as below

----- Before Call By Value -----
 (***x).a = 1
 (*(*(*(x + 1) + 0) + 0)).a = 37

----- After Call By Value -----
 (***x).a = 1
 (*(*(*(x + 1) + 0) + 0)).a = 37

Let us look at examples of Call by Reference

  • Step 1 : Consider a THREE dimensional array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];

There are 6 single dimension arrays in struct ABC x[2][3][4]

x[0][0]

x[0][1]

x[0][2]

x[1][0]

x[1][1]

x[1][2]

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

fun( x[0][0] );

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

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

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

fun( **x );

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

void fun(struct ABC *ptr)
{

}
  • Step 3.2 : Note that it is call by Reference. Means contents of single dimension array can be changed inside function fun

void fun(struct ABC *ptr)
{
  // 1. Address of single dimension array is passed from caller
  // 2. We know that size of each single dimension array is 4 structures
  // 3. Hence, we can change access and change values of 4 structures in this function

  int data = 99;

  for (int i = 0; i < 4; i++)
  {
        ptr[i].a = data++;
        ptr[i].b = data++;
        ptr[i].c = data++;
  }
}
  • See full program below

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC *ptr)
{
  // 1. Address of single dimension array is passed from caller
  // 2. We know that size of each single dimension array is 4 structures
  // 3. Hence, we can change access and change values of 4 structures in this function

  int data = 99;

  for (int i = 0; i < 4; i++)
  {
        ptr[i].a = data++;
        ptr[i].b = data++;
        ptr[i].c = data++;
  }
}

int main(void)
{
        struct ABC x[2][3][4] = {
                {
                        {
                                {.a = 1, .b = 2, .c = 3},
                                {.a = 4, .b = 5, .c = 6},
                                {.a = 7, .b = 8, .c = 9},
                                {.a = 10, .b = 11, .c = 12},
                        },
                        {
                                {.a = 13, .b = 14, .c = 15},
                                {.a = 16, .b = 17, .c = 18},
                                {.a = 19, .b = 20, .c = 21},
                                {.a = 22, .b = 23, .c = 24},
                        },
                        {
                                {.a = 25, .b = 26, .c = 27},
                                {.a = 28, .b = 29, .c = 30},
                                {.a = 31, .b = 32, .c = 33},
                                {.a = 34, .b = 35, .c = 36},
                        },
                },
                {
                        {
                                {.a = 37, .b = 38, .c = 39},
                                {.a = 40, .b = 41, .c = 42},
                                {.a = 43, .b = 44, .c = 45},
                                {.a = 46, .b = 47, .c = 48},
                        },
                        {
                                {.a = 49, .b = 50, .c = 51},
                                {.a = 52, .b = 53, .c = 54},
                                {.a = 55, .b = 56, .c = 57},
                                {.a = 58, .b = 59, .c = 60},
                        },
                        {
                                {.a = 61, .b = 62, .c = 63},
                                {.a = 64, .b = 65, .c = 66},
                                {.a = 67, .b = 68, .c = 69},
                                {.a = 70, .b = 71, .c = 72},
                        },
                },
        };

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 4; i++)
        {
                printf("x[0][0][%d].a = %d ", i, x[0][0][i].a );
                printf("x[0][0][%d].b = %d ", i, x[0][0][i].b );
                printf("x[0][0][%d].c = %d ", i, x[0][0][i].c );

                printf("\n");
        }

        printf("\n");

        for (int i = 0; i < 4; i++)
        {
                printf("x[1][0][%d].a = %d ", i, x[1][0][i].a );
                printf("x[1][0][%d].b = %d ", i, x[1][0][i].b );
                printf("x[1][0][%d].c = %d ", i, x[1][0][i].c );

                printf("\n");
        }

        // Method 1 : Access Single dimension arrays

        fun( x[0][0] );

        fun( x[1][0] );

        // Method 2 : Access Single dimension arrays

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

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

        // Method 3 : Access Single dimension arrays

        fun( **x );

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

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 4; i++)
        {
                printf("x[0][0][%d].a = %d ", i, x[0][0][i].a );
                printf("x[0][0][%d].b = %d ", i, x[0][0][i].b );
                printf("x[0][0][%d].c = %d ", i, x[0][0][i].c );

                printf("\n");
        }

        printf("\n");

        for (int i = 0; i < 4; i++)
        {
                printf("x[1][0][%d].a = %d ", i, x[1][0][i].a );
                printf("x[1][0][%d].b = %d ", i, x[1][0][i].b );
                printf("x[1][0][%d].c = %d ", i, x[1][0][i].c );

                printf("\n");
        }

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
x[0][0][0].a = 1 x[0][0][0].b = 2 x[0][0][0].c = 3
x[0][0][1].a = 4 x[0][0][1].b = 5 x[0][0][1].c = 6
x[0][0][2].a = 7 x[0][0][2].b = 8 x[0][0][2].c = 9
x[0][0][3].a = 10 x[0][0][3].b = 11 x[0][0][3].c = 12

x[1][0][0].a = 37 x[1][0][0].b = 38 x[1][0][0].c = 39
x[1][0][1].a = 40 x[1][0][1].b = 41 x[1][0][1].c = 42
x[1][0][2].a = 43 x[1][0][2].b = 44 x[1][0][2].c = 45
x[1][0][3].a = 46 x[1][0][3].b = 47 x[1][0][3].c = 48

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

x[1][0][0].a = 99 x[1][0][0].b = 100 x[1][0][0].c = 101
x[1][0][1].a = 102 x[1][0][1].b = 103 x[1][0][1].c = 104
x[1][0][2].a = 105 x[1][0][2].b = 106 x[1][0][2].c = 107
x[1][0][3].a = 108 x[1][0][3].b = 109 x[1][0][3].c = 110
  • Step 1 : Consider a THREE dimensional array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];

There are 6 single dimension arrays in struct ABC x[2][3][4]

x[0][0]

x[0][1]

x[0][2]

x[1][0]

x[1][1]

x[1][2]

Address of single dimension arrays is simply

&x[0][0]

&x[0][1]

&x[0][2]

&x[1][0]

&x[1][1]

&x[1][2]

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

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

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

fun( x[0] );

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

fun( *x );

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

void fun(struct ABC (*ptr)[4] )
{

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

void fun(struct ABC (*ptr)[4] )
{
        (*ptr)[0].a = 66; (*ptr)[0].b = 66; (*ptr)[0].c = 66;
        (*ptr)[1].a = 77; (*ptr)[1].b = 77; (*ptr)[1].c = 77;
        (*ptr)[2].a = 88; (*ptr)[2].b = 88; (*ptr)[2].c = 88;
        (*ptr)[3].a = 99; (*ptr)[3].b = 99; (*ptr)[3].c = 99;
}
  • See full program below

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC (*ptr)[4] )
{
        (*ptr)[0].a = 66; (*ptr)[0].b = 66; (*ptr)[0].c = 66;
        (*ptr)[1].a = 77; (*ptr)[1].b = 77; (*ptr)[1].c = 77;
        (*ptr)[2].a = 88; (*ptr)[2].b = 88; (*ptr)[2].c = 88;
        (*ptr)[3].a = 99; (*ptr)[3].b = 99; (*ptr)[3].c = 99;
}

int main(void)
{
        struct ABC x[2][3][4] = {
                {
                        {
                                {.a = 1, .b = 2, .c = 3},
                                {.a = 4, .b = 5, .c = 6},
                                {.a = 7, .b = 8, .c = 9},
                                {.a = 10, .b = 11, .c = 12},
                        },
                        {
                                {.a = 13, .b = 14, .c = 15},
                                {.a = 16, .b = 17, .c = 18},
                                {.a = 19, .b = 20, .c = 21},
                                {.a = 22, .b = 23, .c = 24},
                        },
                        {
                                {.a = 25, .b = 26, .c = 27},
                                {.a = 28, .b = 29, .c = 30},
                                {.a = 31, .b = 32, .c = 33},
                                {.a = 34, .b = 35, .c = 36},
                        },
                },
                {
                        {
                                {.a = 37, .b = 38, .c = 39},
                                {.a = 40, .b = 41, .c = 42},
                                {.a = 43, .b = 44, .c = 45},
                                {.a = 46, .b = 47, .c = 48},
                        },
                        {
                                {.a = 49, .b = 50, .c = 51},
                                {.a = 52, .b = 53, .c = 54},
                                {.a = 55, .b = 56, .c = 57},
                                {.a = 58, .b = 59, .c = 60},
                        },
                        {
                                {.a = 61, .b = 62, .c = 63},
                                {.a = 64, .b = 65, .c = 66},
                                {.a = 67, .b = 68, .c = 69},
                                {.a = 70, .b = 71, .c = 72},
                        },
                },
        };

        printf("----- Before Call By Reference -----\n");
        for (int i = 0; i < 4; i++)
        {
                printf("x[0][0][%d].a = %d ", i, x[0][0][i].a );
                printf("x[0][0][%d].b = %d ", i, x[0][0][i].b );
                printf("x[0][0][%d].c = %d ", i, x[0][0][i].c );

                printf("\n");
        }

        printf("\n");

        for (int i = 0; i < 4; i++)
        {
                printf("x[1][0][%d].a = %d ", i, x[1][0][i].a );
                printf("x[1][0][%d].b = %d ", i, x[1][0][i].b );
                printf("x[1][0][%d].c = %d ", i, x[1][0][i].c );

                printf("\n");
        }

        // Method 1 : Access Single dimension arrays

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

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

        // Method 2 : Access Single dimension arrays

        fun( x[0] );

        fun( x[1] );

        // Method 3 : Access Single dimension arrays

        fun( *x );

        fun( *(x + 1) );

        printf("----- After Call By Reference -----\n");
        for (int i = 0; i < 4; i++)
        {
                printf("x[0][0][%d].a = %d ", i, x[0][0][i].a );
                printf("x[0][0][%d].b = %d ", i, x[0][0][i].b );
                printf("x[0][0][%d].c = %d ", i, x[0][0][i].c );

                printf("\n");
        }

        printf("\n");

        for (int i = 0; i < 4; i++)
        {
                printf("x[1][0][%d].a = %d ", i, x[1][0][i].a );
                printf("x[1][0][%d].b = %d ", i, x[1][0][i].b );
                printf("x[1][0][%d].c = %d ", i, x[1][0][i].c );

                printf("\n");
        }

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
x[0][0][0].a = 1 x[0][0][0].b = 2 x[0][0][0].c = 3
x[0][0][1].a = 4 x[0][0][1].b = 5 x[0][0][1].c = 6
x[0][0][2].a = 7 x[0][0][2].b = 8 x[0][0][2].c = 9
x[0][0][3].a = 10 x[0][0][3].b = 11 x[0][0][3].c = 12

x[1][0][0].a = 37 x[1][0][0].b = 38 x[1][0][0].c = 39
x[1][0][1].a = 40 x[1][0][1].b = 41 x[1][0][1].c = 42
x[1][0][2].a = 43 x[1][0][2].b = 44 x[1][0][2].c = 45
x[1][0][3].a = 46 x[1][0][3].b = 47 x[1][0][3].c = 48

----- After Call By Reference -----
x[0][0][0].a = 66 x[0][0][0].b = 66 x[0][0][0].c = 66
x[0][0][1].a = 77 x[0][0][1].b = 77 x[0][0][1].c = 77
x[0][0][2].a = 88 x[0][0][2].b = 88 x[0][0][2].c = 88
x[0][0][3].a = 99 x[0][0][3].b = 99 x[0][0][3].c = 99

x[1][0][0].a = 66 x[1][0][0].b = 66 x[1][0][0].c = 66
x[1][0][1].a = 77 x[1][0][1].b = 77 x[1][0][1].c = 77
x[1][0][2].a = 88 x[1][0][2].b = 88 x[1][0][2].c = 88
x[1][0][3].a = 99 x[1][0][3].b = 99 x[1][0][3].c = 99
  • Step 1 : Consider a THREE dimensional array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];
  • Step 2 : Pass Double dimension array to function fun

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

void fun(struct ABC (*ptr)[4])
{

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

void fun(struct ABC (*ptr)[4])
{
        // 1. There are 3 single dimension arrays in one double dimension array
        // 2. Each single dimension array has 4 structure objects
        // 3. In total, 12 structures can be accessed and changed in this function

        int data = 66;

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

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC (*ptr)[4])
{
        // 1. There are 3 single dimension arrays in one double dimension array
        // 2. Each single dimension array has 4 structure objects
        // 3. In total, 12 structures can be accessed and changed in this function

        int data = 66;

        for (int i = 0; i < 3; i++)
        {
                for (int j = 0; j < 4; j++)
                {
                        ptr[0][j].a = data++;
                        ptr[0][j].b = data++;
                        ptr[0][j].c = data++;
                }
                ptr++;
        }
}

int main(void)
{
        struct ABC x[2][3][4] = {
                {
                        {
                                {.a = 1, .b = 2, .c = 3},
                                {.a = 4, .b = 5, .c = 6},
                                {.a = 7, .b = 8, .c = 9},
                                {.a = 10, .b = 11, .c = 12},
                        },
                        {
                                {.a = 13, .b = 14, .c = 15},
                                {.a = 16, .b = 17, .c = 18},
                                {.a = 19, .b = 20, .c = 21},
                                {.a = 22, .b = 23, .c = 24},
                        },
                        {
                                {.a = 25, .b = 26, .c = 27},
                                {.a = 28, .b = 29, .c = 30},
                                {.a = 31, .b = 32, .c = 33},
                                {.a = 34, .b = 35, .c = 36},
                        },
                },
                {
                        {
                                {.a = 37, .b = 38, .c = 39},
                                {.a = 40, .b = 41, .c = 42},
                                {.a = 43, .b = 44, .c = 45},
                                {.a = 46, .b = 47, .c = 48},
                        },
                        {
                                {.a = 49, .b = 50, .c = 51},
                                {.a = 52, .b = 53, .c = 54},
                                {.a = 55, .b = 56, .c = 57},
                                {.a = 58, .b = 59, .c = 60},
                        },
                        {
                                {.a = 61, .b = 62, .c = 63},
                                {.a = 64, .b = 65, .c = 66},
                                {.a = 67, .b = 68, .c = 69},
                                {.a = 70, .b = 71, .c = 72},
                        },
                },
        };

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

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

        fun(x[1]);

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

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

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

----- Before Call By Reference -----
x[1][0][0].a = 37 x[1][0][0].b = 38 x[1][0][0].c = 39
x[1][0][1].a = 40 x[1][0][1].b = 41 x[1][0][1].c = 42
x[1][0][2].a = 43 x[1][0][2].b = 44 x[1][0][2].c = 45
x[1][0][3].a = 46 x[1][0][3].b = 47 x[1][0][3].c = 48

x[1][1][0].a = 49 x[1][1][0].b = 50 x[1][1][0].c = 51
x[1][1][1].a = 52 x[1][1][1].b = 53 x[1][1][1].c = 54
x[1][1][2].a = 55 x[1][1][2].b = 56 x[1][1][2].c = 57
x[1][1][3].a = 58 x[1][1][3].b = 59 x[1][1][3].c = 60

x[1][2][0].a = 61 x[1][2][0].b = 62 x[1][2][0].c = 63
x[1][2][1].a = 64 x[1][2][1].b = 65 x[1][2][1].c = 66
x[1][2][2].a = 67 x[1][2][2].b = 68 x[1][2][2].c = 69
x[1][2][3].a = 70 x[1][2][3].b = 71 x[1][2][3].c = 72

----- After Call By Reference -----
x[1][0][0].a = 66 x[1][0][0].b = 67 x[1][0][0].c = 68
x[1][0][1].a = 69 x[1][0][1].b = 70 x[1][0][1].c = 71
x[1][0][2].a = 72 x[1][0][2].b = 73 x[1][0][2].c = 74
x[1][0][3].a = 75 x[1][0][3].b = 76 x[1][0][3].c = 77

x[1][1][0].a = 78 x[1][1][0].b = 79 x[1][1][0].c = 80
x[1][1][1].a = 81 x[1][1][1].b = 82 x[1][1][1].c = 83
x[1][1][2].a = 84 x[1][1][2].b = 85 x[1][1][2].c = 86
x[1][1][3].a = 87 x[1][1][3].b = 88 x[1][1][3].c = 89

x[1][2][0].a = 90 x[1][2][0].b = 91 x[1][2][0].c = 92
x[1][2][1].a = 93 x[1][2][1].b = 94 x[1][2][1].c = 95
x[1][2][2].a = 96 x[1][2][2].b = 97 x[1][2][2].c = 98
x[1][2][3].a = 99 x[1][2][3].b = 100 x[1][2][3].c = 101
  • Step 1 : Consider a THREE dimensional array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];
  • Step 2 : Pass Address of Double dimension array to function fun

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

void fun(struct ABC (*ptr)[3][4])
{
}
  • Step 3.2 : Access and Change structures inside function fun

void fun(struct ABC (*ptr)[3][4])
{
        int data = 666;

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

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC (*ptr)[3][4])
{
        int data = 666;

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

int main(void)
{
        struct ABC x[2][3][4] = {
                {
                        {
                                {.a = 1, .b = 2, .c = 3},
                                {.a = 4, .b = 5, .c = 6},
                                {.a = 7, .b = 8, .c = 9},
                                {.a = 10, .b = 11, .c = 12},
                        },
                        {
                                {.a = 13, .b = 14, .c = 15},
                                {.a = 16, .b = 17, .c = 18},
                                {.a = 19, .b = 20, .c = 21},
                                {.a = 22, .b = 23, .c = 24},
                        },
                        {
                                {.a = 25, .b = 26, .c = 27},
                                {.a = 28, .b = 29, .c = 30},
                                {.a = 31, .b = 32, .c = 33},
                                {.a = 34, .b = 35, .c = 36},
                        },
                },
                {
                        {
                                {.a = 37, .b = 38, .c = 39},
                                {.a = 40, .b = 41, .c = 42},
                                {.a = 43, .b = 44, .c = 45},
                                {.a = 46, .b = 47, .c = 48},
                        },
                        {
                                {.a = 49, .b = 50, .c = 51},
                                {.a = 52, .b = 53, .c = 54},
                                {.a = 55, .b = 56, .c = 57},
                                {.a = 58, .b = 59, .c = 60},
                        },
                        {
                                {.a = 61, .b = 62, .c = 63},
                                {.a = 64, .b = 65, .c = 66},
                                {.a = 67, .b = 68, .c = 69},
                                {.a = 70, .b = 71, .c = 72},
                        },
                },
        };


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

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

        fun(&x[1]);

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

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

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

----- Before Call By Reference -----
x[1][0][0].a = 37 x[1][0][0].b = 38 x[1][0][0].c = 39
x[1][0][1].a = 40 x[1][0][1].b = 41 x[1][0][1].c = 42
x[1][0][2].a = 43 x[1][0][2].b = 44 x[1][0][2].c = 45
x[1][0][3].a = 46 x[1][0][3].b = 47 x[1][0][3].c = 48

x[1][1][0].a = 49 x[1][1][0].b = 50 x[1][1][0].c = 51
x[1][1][1].a = 52 x[1][1][1].b = 53 x[1][1][1].c = 54
x[1][1][2].a = 55 x[1][1][2].b = 56 x[1][1][2].c = 57
x[1][1][3].a = 58 x[1][1][3].b = 59 x[1][1][3].c = 60

x[1][2][0].a = 61 x[1][2][0].b = 62 x[1][2][0].c = 63
x[1][2][1].a = 64 x[1][2][1].b = 65 x[1][2][1].c = 66
x[1][2][2].a = 67 x[1][2][2].b = 68 x[1][2][2].c = 69
x[1][2][3].a = 70 x[1][2][3].b = 71 x[1][2][3].c = 72

----- After Call By Reference -----
x[1][0][0].a = 666 x[1][0][0].b = 667 x[1][0][0].c = 668
x[1][0][1].a = 669 x[1][0][1].b = 670 x[1][0][1].c = 671
x[1][0][2].a = 672 x[1][0][2].b = 673 x[1][0][2].c = 674
x[1][0][3].a = 675 x[1][0][3].b = 676 x[1][0][3].c = 677

x[1][1][0].a = 678 x[1][1][0].b = 679 x[1][1][0].c = 680
x[1][1][1].a = 681 x[1][1][1].b = 682 x[1][1][1].c = 683
x[1][1][2].a = 684 x[1][1][2].b = 685 x[1][1][2].c = 686
x[1][1][3].a = 687 x[1][1][3].b = 688 x[1][1][3].c = 689

x[1][2][0].a = 690 x[1][2][0].b = 691 x[1][2][0].c = 692
x[1][2][1].a = 693 x[1][2][1].b = 694 x[1][2][1].c = 695
x[1][2][2].a = 696 x[1][2][2].b = 697 x[1][2][2].c = 698
x[1][2][3].a = 699 x[1][2][3].b = 700 x[1][2][3].c = 701
  • Step 1 : Consider a THREE dimensional array

struct ABC {
        int a;
        int b;
        int c;
};

struct ABC x[2][3][4];
  • Step 2 : Pass Address of Triple Dimension array to a function

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

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

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

int data = 666;

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].a = data++;
                        (*ptr)[i][j][k].b = data++;
                        (*ptr)[i][j][k].c = data++;
                }
        }
}
  • See full program below

#include <stdio.h>

struct ABC {
        int a;
        int b;
        int c;
};

void fun(struct ABC (*ptr)[2][3][4] )
{
        //Access and change individual structures

        int data = 666;

        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].a = data++;
                                (*ptr)[i][j][k].b = data++;
                                (*ptr)[i][j][k].c = data++;
                        }
                }
        }
}

int main(void)
{
        struct ABC x[2][3][4] = {
                {
                        {
                                {.a = 1, .b = 2, .c = 3},
                                {.a = 4, .b = 5, .c = 6},
                                {.a = 7, .b = 8, .c = 9},
                                {.a = 10, .b = 11, .c = 12},
                        },
                        {
                                {.a = 13, .b = 14, .c = 15},
                                {.a = 16, .b = 17, .c = 18},
                                {.a = 19, .b = 20, .c = 21},
                                {.a = 22, .b = 23, .c = 24},
                        },
                        {
                                {.a = 25, .b = 26, .c = 27},
                                {.a = 28, .b = 29, .c = 30},
                                {.a = 31, .b = 32, .c = 33},
                                {.a = 34, .b = 35, .c = 36},
                        },
                },
                {
                        {
                                {.a = 37, .b = 38, .c = 39},
                                {.a = 40, .b = 41, .c = 42},
                                {.a = 43, .b = 44, .c = 45},
                                {.a = 46, .b = 47, .c = 48},
                        },
                        {
                                {.a = 49, .b = 50, .c = 51},
                                {.a = 52, .b = 53, .c = 54},
                                {.a = 55, .b = 56, .c = 57},
                                {.a = 58, .b = 59, .c = 60},
                        },
                        {
                                {.a = 61, .b = 62, .c = 63},
                                {.a = 64, .b = 65, .c = 66},
                                {.a = 67, .b = 68, .c = 69},
                                {.a = 70, .b = 71, .c = 72},
                        },
                },
        };

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

        fun(&x);

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

        return 0;
}
  • Output is as below

----- Before Call By Reference -----
x[0][0][0].a = 37 x[0][0][0].b = 38 x[0][0][0].c = 39
x[0][0][1].a = 37 x[0][0][1].b = 38 x[0][0][1].c = 39
x[0][0][2].a = 37 x[0][0][2].b = 38 x[0][0][2].c = 39
x[0][0][3].a = 37 x[0][0][3].b = 38 x[0][0][3].c = 39

x[0][1][0].a = 40 x[0][1][0].b = 41 x[0][1][0].c = 42
x[0][1][1].a = 40 x[0][1][1].b = 41 x[0][1][1].c = 42
x[0][1][2].a = 40 x[0][1][2].b = 41 x[0][1][2].c = 42
x[0][1][3].a = 40 x[0][1][3].b = 41 x[0][1][3].c = 42

x[0][2][0].a = 43 x[0][2][0].b = 44 x[0][2][0].c = 45
x[0][2][1].a = 43 x[0][2][1].b = 44 x[0][2][1].c = 45
x[0][2][2].a = 43 x[0][2][2].b = 44 x[0][2][2].c = 45
x[0][2][3].a = 43 x[0][2][3].b = 44 x[0][2][3].c = 45


x[1][0][0].a = 49 x[1][0][0].b = 50 x[1][0][0].c = 51
x[1][0][1].a = 49 x[1][0][1].b = 50 x[1][0][1].c = 51
x[1][0][2].a = 49 x[1][0][2].b = 50 x[1][0][2].c = 51
x[1][0][3].a = 49 x[1][0][3].b = 50 x[1][0][3].c = 51

x[1][1][0].a = 52 x[1][1][0].b = 53 x[1][1][0].c = 54
x[1][1][1].a = 52 x[1][1][1].b = 53 x[1][1][1].c = 54
x[1][1][2].a = 52 x[1][1][2].b = 53 x[1][1][2].c = 54
x[1][1][3].a = 52 x[1][1][3].b = 53 x[1][1][3].c = 54

x[1][2][0].a = 55 x[1][2][0].b = 56 x[1][2][0].c = 57
x[1][2][1].a = 55 x[1][2][1].b = 56 x[1][2][1].c = 57
x[1][2][2].a = 55 x[1][2][2].b = 56 x[1][2][2].c = 57
x[1][2][3].a = 55 x[1][2][3].b = 56 x[1][2][3].c = 57

----- After Call By Reference -----
x[0][0][0].a = 702 x[0][0][0].b = 703 x[0][0][0].c = 704
x[0][0][1].a = 702 x[0][0][1].b = 703 x[0][0][1].c = 704
x[0][0][2].a = 702 x[0][0][2].b = 703 x[0][0][2].c = 704
x[0][0][3].a = 702 x[0][0][3].b = 703 x[0][0][3].c = 704

x[0][1][0].a = 705 x[0][1][0].b = 706 x[0][1][0].c = 707
x[0][1][1].a = 705 x[0][1][1].b = 706 x[0][1][1].c = 707
x[0][1][2].a = 705 x[0][1][2].b = 706 x[0][1][2].c = 707
x[0][1][3].a = 705 x[0][1][3].b = 706 x[0][1][3].c = 707

x[0][2][0].a = 708 x[0][2][0].b = 709 x[0][2][0].c = 710
x[0][2][1].a = 708 x[0][2][1].b = 709 x[0][2][1].c = 710
x[0][2][2].a = 708 x[0][2][2].b = 709 x[0][2][2].c = 710
x[0][2][3].a = 708 x[0][2][3].b = 709 x[0][2][3].c = 710


x[1][0][0].a = 714 x[1][0][0].b = 715 x[1][0][0].c = 716
x[1][0][1].a = 714 x[1][0][1].b = 715 x[1][0][1].c = 716
x[1][0][2].a = 714 x[1][0][2].b = 715 x[1][0][2].c = 716
x[1][0][3].a = 714 x[1][0][3].b = 715 x[1][0][3].c = 716

x[1][1][0].a = 717 x[1][1][0].b = 718 x[1][1][0].c = 719
x[1][1][1].a = 717 x[1][1][1].b = 718 x[1][1][1].c = 719
x[1][1][2].a = 717 x[1][1][2].b = 718 x[1][1][2].c = 719
x[1][1][3].a = 717 x[1][1][3].b = 718 x[1][1][3].c = 719

x[1][2][0].a = 720 x[1][2][0].b = 721 x[1][2][0].c = 722
x[1][2][1].a = 720 x[1][2][1].b = 721 x[1][2][1].c = 722
x[1][2][2].a = 720 x[1][2][2].b = 721 x[1][2][2].c = 722
x[1][2][3].a = 720 x[1][2][3].b = 721 x[1][2][3].c = 722