Functions and Structure Single Dimension Array ================================================= In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the calling conventions of structure single dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Call by Value .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Call by Reference .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Revisit Basics : :doc:`../../array_n_ptrs/arrays_n_structs/struct_sd_array` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Structure Single Dimension Array : Syntax ` * :ref:`Structure Single Dimension Array : FAQs ` * :ref:`Structure Single Dimension Array : fun(expression) ` * :ref:`Rules for Call By Value ` * :ref:`Rules for Call By Reference ` * :ref:`Examples of Call by Value ` * :ref:`Example 1 : Call by Value with [ ] ` * :ref:`Example 2 : Call by Value with * ` * :ref:`Examples of Call by Reference ` * :ref:`Example 3 : Call by Reference WITH &x[ ] ` * :ref:`Example 4 : Call by Reference WITH (x + n) ` * :ref:`Example 5 : Pass full array to a function ` * :ref:`Example 6 : Pass part of the array by reference ` * :ref:`Example 7 : Address of full array and Pointer to an Array ` .. _funcs_n_ptrs_struct_sd_array_ex_0_0: .. tab-set:: .. tab-item:: Structure Single Dimension Array : Syntax .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow .. code-block:: c struct ABC { type1 member1; type2 member2; type3 member3; }; struct ABC array_name[Column]; .. _funcs_n_ptrs_struct_sd_array_ex_0_1: .. tab-set:: .. tab-item:: Structure Single Dimension Array : FAQs Consider a Structure Single Dimension Array .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5]; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us answer few basic questions in this array .. _funcs_n_ptrs_struct_sd_array_ex_6: .. tab-set:: .. tab-item:: Structure Single Dimension Array : fun(expression) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow If ``fun(x)`` is the function call, then ``fun(typeof(x))`` is the prototype / definition ================== ====================================== ========================================================================== Function Call Function Definition Observations ================== ====================================== ========================================================================== fun(x[0]) void fun(struct ABC x) { } * Call by Value fun(x[1]) void fun(struct ABC x) { } * Call by Value fun(x[4]) void fun(struct ABC x) { } * Call by Value fun(&x[0]) void fun(struct ABC \*p) { } * Call by Reference fun(&x[1]) void fun(struct ABC \*p) { } * Call by Reference fun(&x[4]) void fun(struct ABC \*p) { } * Call by Reference fun(\*x) void fun(struct ABC x) { } * Call by Value fun(\*(x + 1)) void fun(struct ABC x) { } * Call by Value fun(\*(x + 4)) void fun(struct ABC x) { } * Call by Value fun(x) void fun(struct ABC \*p) { } * Call by Reference fun(x + 1) void fun(struct ABC \*p) { } * Call by Reference fun(x + 4) void fun(struct ABC \*p) { } * Call by Reference fun(&x) void fun(struct ABC (\*p) [4] ) { } * Call by Reference ================== ====================================== ========================================================================== .. _funcs_n_ptrs_struct_sd_array_rules_for_call_by_value: .. tab-set:: .. tab-item:: Rules for Call by Value .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow If Declaration has ONE dereference operator, and * Expression has ONE dereference operator [], and * Expression does not have ``&`` * then it is call by value .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow If Declaration has ONE dereference operators, and * Expression has ONE dereference operator \*, and * Expression does not have ``&`` * then it is call by value .. _funcs_n_ptrs_struct_sd_array_rules_for_call_by_reference: .. tab-set:: .. tab-item:: Rules for Call by Reference .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow If Declaration has ONE dereference operator, and * Expression has ONE dereference operators [] or \*, and * Expression has ONE & * then it is call by reference * Example : &x[0] .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow If Declaration has ONE dereference operator, and * Expression has ZERO dereference operator [ ] or \*, and * Expression has ZERO & operator * then it is call by reference * Example : x + 1, x + 4 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow If Declaration has ONE dereference operator, and * Expression has ZERO dereference operator [ ] or \*, and * Expression has ONE & operator * then it is call by reference * Example : &x .. _funcs_n_ptrs_struct_sd_array_examples_call_by_value: .. tab-set:: .. tab-item:: Examples of Call by Value .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us look at examples of Call by Value .. _funcs_n_ptrs_struct_sd_array_ex_7: .. tab-set:: .. tab-item:: Example 1 : Call by Value with [ ] .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Example for Call By Value with [ ] * Step 1 : Define a structure array ``x`` .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5] = { {.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}, }; * Step 2 : Pass an individual structure ``x[2]`` to a function. Call by Value .. code-block:: c fun(x[2]); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Individual array elements can be accessed using [ ] .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow In this case ``x[2]`` is third structure in the array .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``x[2]`` is fully dereferenced and there is no ``&`` symbol in ``fun(x[2])``. Hence this is Call By Value * Step 3 : Define function ``fun`` .. code-block:: c void fun(struct ABC x) { } * Step 4 : Change value of ``x`` inside function ``fun`` .. code-block:: c void fun(struct ABC x) { x.a = 100; x.b = 101; x.c = 102; } * See the full program below .. code-block:: c #include struct ABC { int a; int b; int c; }; void fun(struct ABC x) { x.a = 100; x.b = 101; x.c = 102; } int main(void) { struct ABC x[5] = { {.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}, }; printf("----- Before Call By Value -----\n"); printf("x[2].a = %d\n", x[2].a); printf("x[2].b = %d\n", x[2].b); printf("x[2].c = %d\n", x[2].c); fun(x[2]); printf("----- After Call By Value -----\n"); printf("x[2].a = %d\n", x[2].a); printf("x[2].b = %d\n", x[2].b); printf("x[2].c = %d\n", x[2].c); return 0; } * Output is as below .. code-block:: c ----- Before Call By Value ----- x[2].a = 7 x[2].b = 8 x[2].c = 9 ----- After Call By Value ----- x[2].a = 7 x[2].b = 8 x[2].c = 9 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Changing value of ``x`` inside function ``fun`` DOES NOT change ``a[2]`` in array ``a`` .. _funcs_n_ptrs_struct_sd_array_ex_8: .. tab-set:: .. tab-item:: Example 2 : Call by Value with ``*`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Example for Call By Value with ``*`` * Step 1 : Define a structure array ``x`` .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5] = { {.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}, }; * Step 2 : Pass an individual structure ``*(x + 2)`` to a function. Call by Value .. code-block:: c fun( *(x + 2) ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Individual array elements can be accessed using ``*`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow In this case ``*(x + 2)`` is third structure in the array .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``*(x + 2)`` is fully dereferenced and there is no ``&`` symbol in ``fun( *(x + 2) )``. Hence this is Call By Value * Step 3 : Define function ``fun`` .. code-block:: c void fun(struct ABC x) { } * Step 4 : Change value of ``x`` inside function ``fun`` .. code-block:: c void fun(struct ABC x) { x.a = 100; x.b = 101; x.c = 102; } * See the full program below .. code-block:: c #include struct ABC { int a; int b; int c; }; void fun(struct ABC x) { x.a = 100; x.b = 101; x.c = 102; } int main(void) { struct ABC x[5] = { {.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}, }; printf("----- Before Call By Value -----\n"); printf(" (*(x + 2)).a = %d\n", (*(x + 2)).a ); printf(" (*(x + 2)).b = %d\n", (*(x + 2)).b ); printf(" (*(x + 2)).c = %d\n", (*(x + 2)).c ); fun( *(x + 2) ); printf("----- After Call By Value -----\n"); printf(" (*(x + 2)).a = %d\n", (*(x + 2)).a ); printf(" (*(x + 2)).b = %d\n", (*(x + 2)).b ); printf(" (*(x + 2)).c = %d\n", (*(x + 2)).c ); return 0; } * Output is as below .. code-block:: c ----- Before Call By Value ----- (*(x + 2)).a = 7 (*(x + 2)).b = 8 (*(x + 2)).c = 9 ----- After Call By Value ----- (*(x + 2)).a = 7 (*(x + 2)).b = 8 (*(x + 2)).c = 9 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Changing value of ``x`` inside function ``fun`` DOES NOT change ``*(x + 2)`` in array ``x`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Remember ``x[2]`` and ``*(x + 2)`` are one and the same .. _funcs_n_ptrs_struct_sd_array_examples_call_by_reference: .. tab-set:: .. tab-item:: Examples of Call by Reference .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us look at examples of Call by Reference .. _funcs_n_ptrs_struct_sd_array_ex_9: .. tab-set:: .. tab-item:: Example 3 : Call by Reference WITH &x[ ] .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Example for Call By Reference with ``&x[ ]`` * Step 1 : Define a structure array ``x`` .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5] = { {.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}, }; * Step 2 : Pass address of an individual structure ``&x[2]`` to a function. Call by Reference .. code-block:: c fun( &x[2] ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Address of individual array elements can be accessed using ``&`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow In this case ``&x[2]`` is the address of third structure in the array .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Since we are passing address of third structure to function ``fun``, it is called call by reference with respect to third structure * Step 3 : Define function ``fun`` .. code-block:: c void fun(struct ABC *x) { } * Step 4 : Change value of ``*x`` inside function ``fun`` .. code-block:: c void fun(struct ABC *x) { x->a = 777; x->b = 888; x->c = 999; } * See the full program below .. code-block:: c #include struct ABC { int a; int b; int c; }; void fun(struct ABC *x) { x->a = 777; x->b = 888; x->c = 999; } int main(void) { struct ABC x[5] = { {.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}, }; printf("----- Before Call By Reference -----\n"); printf("x[2].a = %d\n", x[2].a); printf("x[2].b = %d\n", x[2].b); printf("x[2].c = %d\n", x[2].c); fun( &x[2] ); printf("----- After Call By Reference -----\n"); printf("x[2].a = %d\n", x[2].a); printf("x[2].b = %d\n", x[2].b); printf("x[2].c = %d\n", x[2].c); return 0; } * Output is as below .. code-block:: c ----- Before Call By Reference ----- x[2].a = 7 x[2].b = 8 x[2].c = 9 ----- After Call By Reference ----- x[2].a = 777 x[2].b = 888 x[2].c = 999 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Changing value of ``*x`` inside function ``fun`` CHANGES ``x[2]`` in array ``x`` .. _funcs_n_ptrs_struct_sd_array_ex_10: .. tab-set:: .. tab-item:: Example 4 : Call by Reference WITH ``(x + n)`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Example for Call By Reference with ``(x + n)`` * Step 1 : Define a structure array ``x`` .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5] = { {.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}, }; * Step 2 : Pass address of individual structure ``x + 2`` to a function. Call by Reference .. code-block:: c fun( x + 2 ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow In this case ``x + 2`` is the address of third structure in the array .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Since we are passing address of third structure to function ``fun``, it is called call by reference with respect to third structure * Step 3 : Define function ``fun`` .. code-block:: c void fun(struct ABC *x) { } * Step 4 : Change value of ``*x`` inside function ``fun`` .. code-block:: c void fun(struct ABC *x) { x->a = 777; x->b = 888; x->c = 999; } * See the full program below .. code-block:: c #include struct ABC { int a; int b; int c; }; void fun(struct ABC *x) { x->a = 777; x->b = 888; x->c = 999; } int main(void) { struct ABC x[5] = { {.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}, }; printf("----- Before Call By Reference -----\n"); printf("x[2].a = %d\n", x[2].a); printf("x[2].b = %d\n", x[2].b); printf("x[2].c = %d\n", x[2].c); fun( x + 2 ); printf("----- After Call By Reference -----\n"); printf("x[2].a = %d\n", x[2].a); printf("x[2].b = %d\n", x[2].b); printf("x[2].c = %d\n", x[2].c); return 0; } * Output is as below .. code-block:: c ----- Before Call By Reference ----- x[2].a = 7 x[2].b = 8 x[2].c = 9 ----- After Call By Reference ----- x[2].a = 777 x[2].b = 888 x[2].c = 999 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Changing value of ``*x`` inside function ``fun`` CHANGES ``x[2]`` in array ``x`` .. _funcs_n_ptrs_struct_sd_array_ex_12: .. tab-set:: .. tab-item:: Example 5 : Pass full array array to a function * Step 1 : Consider a structure array .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5] = { {.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}, }; * Step 2 : Pass full array array to a function .. code-block:: c fun( x ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Note that we are passing starting address of array .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Hence function ``fun`` has read and write access to all Bytes of array * Step 3 : Define a function .. code-block:: c void fun(struct ABC *x) { for (int i = 0; i < 5; i++) { x[i].a = 777; x[i].b = 888; x[i].c = 999; } } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow function ``fun`` has access to all structures * See full program below .. code-block:: c #include struct ABC { int a; int b; int c; }; void fun(struct ABC *x) { for (int i = 0; i < 5; i++) { x[i].a = 777; x[i].b = 888; x[i].c = 999; } } int main(void) { struct ABC x[5] = { {.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}, }; printf("----- Before Call By Reference -----\n"); for (int i = 0; i < 5; i++) { printf("x[%d].a = %d ", i, x[i].a); printf("x[%d].b = %d ", i, x[i].b); printf("x[%d].c = %d ", i, x[i].c); printf("\n"); } fun(x); printf("----- After Call By Reference -----\n"); for (int i = 0; i < 5; i++) { printf("x[%d].a = %d ", i, x[i].a); printf("x[%d].b = %d ", i, x[i].b); printf("x[%d].c = %d ", i, x[i].c); printf("\n"); } return 0; } * Output is as below .. code-block:: c ----- Before Call By Reference ----- x[0].a = 1 x[0].b = 2 x[0].c = 3 x[1].a = 4 x[1].b = 5 x[1].c = 6 x[2].a = 7 x[2].b = 8 x[2].c = 9 x[3].a = 10 x[3].b = 11 x[3].c = 12 x[4].a = 13 x[4].b = 14 x[4].c = 15 ----- After Call By Reference ----- x[0].a = 777 x[0].b = 888 x[0].c = 999 x[1].a = 777 x[1].b = 888 x[1].c = 999 x[2].a = 777 x[2].b = 888 x[2].c = 999 x[3].a = 777 x[3].b = 888 x[3].c = 999 x[4].a = 777 x[4].b = 888 x[4].c = 999 .. _funcs_n_ptrs_struct_sd_array_ex_13: .. tab-set:: .. tab-item:: Example 6 : Pass part of the array by reference * Step 1 : Consider a structure array .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5] = { {.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}, }; * Step 2 : Pass full array by reference .. code-block:: c fun( a + 2 ); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Note that we are passing part of the array by reference .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow In this case, we are passing address of 3rd structure .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Hence function ``fun`` has read and write access to structures at indexes 2,3,4 in forward direction .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Hence function ``fun`` has read and write access to structures at indexes 0, 1 in backward direction * Step 3 : Define a function .. code-block:: c void fun(struct ABC *ptr) { ptr[-2].a = 55; // Same as x[0] ptr[-1].a = 66; // Same as x[1] ptr[0].a = 77; // Same as x[2] ptr[1].a = 88; // Same as x[3] ptr[2].a = 99; // Same as x[4] } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Note the relative access mechanism used inside function ``fun`` * See full program below .. code-block:: c #include struct ABC { int a; int b; int c; }; void fun(struct ABC *ptr) { ptr[-2].a = 55; // Same as x[0] ptr[-1].a = 66; // Same as x[1] ptr[0].a = 77; // Same as x[2] ptr[1].a = 88; // Same as x[3] ptr[2].a = 99; // Same as x[4] } int main(void) { struct ABC x[5] = { {.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}, }; printf("----- Before Call By Reference -----\n"); for (int i = 0; i < 5; i++) { printf("x[%d].a = %d ", i, x[i].a); printf("x[%d].b = %d ", i, x[i].b); printf("x[%d].c = %d ", i, x[i].c); printf("\n"); } fun(x + 2); printf("----- After Call By Reference -----\n"); for (int i = 0; i < 5; i++) { printf("x[%d].a = %d ", i, x[i].a); printf("x[%d].b = %d ", i, x[i].b); printf("x[%d].c = %d ", i, x[i].c); printf("\n"); } return 0; } * Output is as below .. code-block:: c ----- Before Call By Reference ----- x[0].a = 1 x[0].b = 2 x[0].c = 3 x[1].a = 4 x[1].b = 5 x[1].c = 6 x[2].a = 7 x[2].b = 8 x[2].c = 9 x[3].a = 10 x[3].b = 11 x[3].c = 12 x[4].a = 13 x[4].b = 14 x[4].c = 15 ----- After Call By Reference ----- x[0].a = 55 x[0].b = 2 x[0].c = 3 x[1].a = 66 x[1].b = 5 x[1].c = 6 x[2].a = 77 x[2].b = 8 x[2].c = 9 x[3].a = 88 x[3].b = 11 x[3].c = 12 x[4].a = 99 x[4].b = 14 x[4].c = 15 .. _funcs_n_ptrs_struct_sd_array_ex_14: .. tab-set:: .. tab-item:: Example 7 : Address of full array and Pointer to an Array * Step 1 : Consider a structure array .. code-block:: c struct ABC { int a; int b; int c; }; struct ABC x[5] = { {.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}, }; * Step 2 : Pass the address of array ``a`` to function ``fun`` .. code-block:: c fun(&a); * Step 3 : Define the function ``fun`` .. code-block:: c void fun( struct ABC (*ptr)[5] ) { } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Note that ``struct ABC (*ptr)[5]`` is pointer to an array of 5 structures .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Which means incrementing ``ptr`` will increment by 5 Structures and decrementing ``ptr`` will decrement by 5 Structures * Step 4 : Access individual structures inside function ``fun`` .. code-block:: c void fun( struct ABC (*ptr)[10] ) { //Change individual structures (*ptr)[0].a = 55; (*ptr)[1].a = 66; (*ptr)[2].a = 77; (*ptr)[3].a = 88; (*ptr)[4].a = 99; } * See the full program below .. code-block:: c #include struct ABC { int a; int b; int c; }; void fun( struct ABC (*ptr)[5] ) { //Change individual structures (*ptr)[0].a = 55; (*ptr)[1].a = 66; (*ptr)[2].a = 77; (*ptr)[3].a = 88; (*ptr)[4].a = 99; } int main(void) { struct ABC x[5] = { {.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}, }; printf("----- Before Call By Reference -----\n"); for (int i = 0; i < 5; i++) { printf("x[%d].a = %d ", i, x[i].a); printf("x[%d].b = %d ", i, x[i].b); printf("x[%d].c = %d ", i, x[i].c); printf("\n"); } fun(&x); printf("----- After Call By Reference -----\n"); for (int i = 0; i < 5; i++) { printf("x[%d].a = %d ", i, x[i].a); printf("x[%d].b = %d ", i, x[i].b); printf("x[%d].c = %d ", i, x[i].c); printf("\n"); } return 0; } * Output is as below .. code-block:: c ----- Before Call By Reference ----- x[0].a = 1 x[0].b = 2 x[0].c = 3 x[1].a = 4 x[1].b = 5 x[1].c = 6 x[2].a = 7 x[2].b = 8 x[2].c = 9 x[3].a = 10 x[3].b = 11 x[3].c = 12 x[4].a = 13 x[4].b = 14 x[4].c = 15 ----- After Call By Reference ----- x[0].a = 55 x[0].b = 2 x[0].c = 3 x[1].a = 66 x[1].b = 5 x[1].c = 6 x[2].a = 77 x[2].b = 8 x[2].c = 9 x[3].a = 88 x[3].b = 11 x[3].c = 12 x[4].a = 99 x[4].b = 14 x[4].c = 15 .. card:: See Also * Other topics of structure and functions * :doc:`./struct` * :doc:`./struct_sd_array` * :doc:`./struct_dd_array` * :doc:`./struct_td_array` * :doc:`./struct_sp` * :doc:`./struct_dp` * :doc:`./struct_tp` * Current Module * :doc:`../funcs_n_ptrs` * Previous Module * :doc:`../../typecasting_n_ptr/typecasting_n_ptr` * Next Module * :doc:`../../memcpy_ptr/memcpy_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :doc:`../../malloc_ptr/malloc_ptr` * :doc:`../../const_ptr/const_ptr` * :doc:`../../void_ptr/void_ptr` * :doc:`../../array_of_ptr/array_of_ptr` * :doc:`../../ptr_to_array/ptr_to_array` * :doc:`../../function_ptr/function_ptr` * :doc:`../../pre_incr_ptr/pre_incr_ptr` * :doc:`../../post_incr_ptr/post_incr_ptr` * :doc:`../../pre_decr_ptr/pre_decr_ptr` * :doc:`../../post_decr_ptr/post_decr_ptr`