Basics of Structure Double 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 basic properties of a structure double dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the different expressions of double dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are synonymous expresions of double dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to find sizeof() of expressions of double dimension array ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Structure Double Dimension Array : Syntax ` * :ref:`Structure Double Dimension Array : FAQs ` * :ref:`Structure Double Dimension Array : Basic Examples ` * :ref:`Structure Double Dimension Array : Basic Example 1 ` * :ref:`Structure Double Dimension Array : Basic Properties ` * :ref:`Structure Double Dimension Array : Basic Expressions ` * :ref:`Structure Double Dimension Array : Synonym Expressions ` * :ref:`Structure Double Dimension Array : typeof(expression) ` * :ref:`Structure Double Dimension Array : sizeof(expression) ` * :ref:`Structure Double Dimension Array : fun(expression) ` * :ref:`Structure Double Dimension Array : All Examples of Function Calls ` .. _array_n_ptrs_struct_dd_array_ex_0_0: .. tab-set:: .. tab-item:: Structure Double Dimension Array : Syntax .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow struct XYZ array_name[Row][Column]; .. _array_n_ptrs_struct_dd_array_ex_0_1: .. tab-set:: .. tab-item:: Structure Double Dimension Array : FAQs Consider a structure double dimension array .. code-block:: c struct XYZ { int x; int y; int z; }; struct XYZ a[3][4]; .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us answer few basic questions in this array .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many structures can be stored in this array ? .. dropdown:: See Answer Number of Structures = Rows * Columns = 3 * 4 = 12 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many bytes are there in this array ? .. dropdown:: See Answer Number of Bytes = Rows * Columns * sizeof(struct XYZ) = 3 * 4 * 12 = 144 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the sizeof the array ? .. dropdown:: See Answer sizeof(a) = Number of Bytes = Rows * Columns * sizeof(struct XYZ) = 3 * 4 * 12 = 144 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How many single dimension arrays are present in this double dimension array ? .. dropdown:: See Answer There are 3 Single dimension arrays. Size of each single dimension array is 48 Bytes .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the names of single dimension arrays in this double dimension array ? .. dropdown:: See Answer a[0] a[1] a[2] .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How do you represent the first structure ? .. dropdown:: See Answer a[0][0] .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How do you represent the last structure ? .. dropdown:: See Answer a[2][3] .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How do you initialise the array at the time of declaration ? .. dropdown:: See Answer .. code-block:: c struct XYZ a[3][4] = { { {.x = 1, .y = 1, .z = 1}, {.x = 2, .y = 2, .z = 2}, {.x = 3, .y = 3, .z = 3}, {.x = 4, .y = 4, .z = 4}, }, { {.x = 11, .y = 11, .z = 11}, {.x = 22, .y = 22, .z = 22}, {.x = 33, .y = 33, .z = 33}, {.x = 44, .y = 44, .z = 44}, }, { {.x = 111, .y = 111, .z = 111}, {.x = 222, .y = 222, .z = 222}, {.x = 333, .y = 333, .z = 333}, {.x = 444, .y = 444, .z = 444}, }, }; .. _array_n_ptrs_struct_dd_array_ex_0_2: .. tab-set:: .. tab-item:: Structure Double Dimension Array : Basic Examples .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us now explore basic examples of double dimension array ! .. _array_n_ptrs_struct_dd_array_ex_0_3: .. tab-set:: .. tab-item:: Structure Double Dimension Array : Basic Example 1 * Step 1 : Define a Double Dimension Array .. code-block:: c struct XYZ { int x; int y; int z; }; struct XYZ a[3][4] = { { {.x = 1, .y = 1, .z = 1}, {.x = 2, .y = 2, .z = 2}, {.x = 3, .y = 3, .z = 3}, {.x = 4, .y = 4, .z = 4}, }, { {.x = 11, .y = 11, .z = 11}, {.x = 22, .y = 22, .z = 22}, {.x = 33, .y = 33, .z = 33}, {.x = 44, .y = 44, .z = 44}, }, { {.x = 111, .y = 111, .z = 111}, {.x = 222, .y = 222, .z = 222}, {.x = 333, .y = 333, .z = 333}, {.x = 444, .y = 444, .z = 444}, }, }; * Step 2 : Access and Print individual structures .. code-block:: c for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { printf("a[%d][%d].x = %d ", i, j, a[i][j].x); printf("a[%d][%d].y = %d ", i, j, a[i][j].y); printf("a[%d][%d].z = %d ", i, j, a[i][j].z); printf("\n"); } printf("\n"); } * Step 3 : See the full program below .. code-block:: c #include struct XYZ { int x; int y; int z; }; int main(void) { struct XYZ a[3][4] = { { {.x = 1, .y = 1, .z = 1}, {.x = 2, .y = 2, .z = 2}, {.x = 3, .y = 3, .z = 3}, {.x = 4, .y = 4, .z = 4}, }, { {.x = 11, .y = 11, .z = 11}, {.x = 22, .y = 22, .z = 22}, {.x = 33, .y = 33, .z = 33}, {.x = 44, .y = 44, .z = 44}, }, { {.x = 111, .y = 111, .z = 111}, {.x = 222, .y = 222, .z = 222}, {.x = 333, .y = 333, .z = 333}, {.x = 444, .y = 444, .z = 444}, }, }; for (int i = 0; i < 3; i++) { for (int j = 0; j < 4; j++) { printf("a[%d][%d].x = %d ", i, j, a[i][j].x); printf("a[%d][%d].y = %d ", i, j, a[i][j].y); printf("a[%d][%d].z = %d ", i, j, a[i][j].z); printf("\n"); } printf("\n"); } return 0; } * Step 4 : Output is as below .. code-block:: c a[0][0].x = 1 a[0][0].y = 1 a[0][0].z = 1 a[0][1].x = 2 a[0][1].y = 2 a[0][1].z = 2 a[0][2].x = 3 a[0][2].y = 3 a[0][2].z = 3 a[0][3].x = 4 a[0][3].y = 4 a[0][3].z = 4 a[1][0].x = 11 a[1][0].y = 11 a[1][0].z = 11 a[1][1].x = 22 a[1][1].y = 22 a[1][1].z = 22 a[1][2].x = 33 a[1][2].y = 33 a[1][2].z = 33 a[1][3].x = 44 a[1][3].y = 44 a[1][3].z = 44 a[2][0].x = 111 a[2][0].y = 111 a[2][0].z = 111 a[2][1].x = 222 a[2][1].y = 222 a[2][1].z = 222 a[2][2].x = 333 a[2][2].y = 333 a[2][2].z = 333 a[2][3].x = 444 a[2][3].y = 444 a[2][3].z = 444 .. _array_n_ptrs_struct_dd_array_ex_1: .. tab-set:: .. tab-item:: Structure Double Dimension Array : Basic Properties .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us now explore double dimension array in depth ! Below are the properties * Expressions * Synonyms * typeof(expression) * sizeof(expression) * fun(expression) .. _array_n_ptrs_struct_dd_array_ex_2: .. tab-set:: .. tab-item:: Structure Double Dimension Array : Basic Expressions .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Know what are the possible expressions and how to properly call them ! =================== ====================================================== Expression Description =================== ====================================================== a[0][0] * ``a[0][0]`` is a structure a[1][0] * ``a[1][0]`` is a structure a[2][0] * ``a[2][0]`` is a structure &a[0][0] * ``&a[0][0]`` is address of a structure &a[1][0] * ``&a[1][0]`` is address of a structure &a[2][0] * ``&a[2][0]`` is address of a structure \*\*a * \*\*a is structure. Same as a[0][0] \*(\*(a + 1) + 0) * \*(\*(a + 1) + 0) is a structure. Same as a[1][0] \*(\*(a + 2) + 0) * \*(\*(a + 2) + 0) is a structure. Same as a[2][0] a[0] * ``a[0]`` is a single dimension array a[1] * ``a[1]`` is a single dimension array a[2] * ``a[2]`` is a single dimension array &a[0] * ``&a[0]`` is address of a single dimension array &a[1] * ``&a[1]`` is address of a single dimension array &a[2] * ``&a[2]`` is address of a single dimension array \*a * ``*a`` is a single dimension array \*(a + 1) * ``*(a + 1)`` is a single dimension array \*(a + 2) * ``*(a + 2)`` is a single dimension array a * ``a`` is address of a single dimension array a + 1 * ``a + 1`` is address of a single dimension array a + 2 * ``a + 2`` is address of a single dimension array &a * ``&a`` is the address of double dimension array =================== ====================================================== .. _array_n_ptrs_struct_dd_array_ex_3: .. tab-set:: .. tab-item:: Structure Double Dimension Array : Synonym Expressions .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Synonyms : Which can be used interchangeably ! .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Programmatically few expressions are one and the same ! Let us learn them =================== ======================================== Expression Synonyms =================== ======================================== a[0][0] \*\*a a[1][0] \*(\*(a + 1) + 0) a[2][0] \*(\*(a + 2) + 0) &a[0][0] a[0] &a[1][0] a[1] &a[2][0] a[2] \*\*a a[0][0] \*(\*(a + 1) + 0) a[1][0] \*(\*(a + 2) + 0) a[2][0] a[0] \*a &a[0][0] a[1] \*(a + 1) &a[1][0] a[2] \*(a + 2) &a[2][0] &a[0] a + 0 a &a[1] a + 1 &a[2] a + 2 \*a a[0] &a[0][0] \*(a + 1) a[1] &a[1][0] \*(a + 2) a[2] &a[2][0] a &a[0] a + 1 &a[1] a + 2 &a[2] &a &a =================== ======================================== .. _array_n_ptrs_struct_dd_array_ex_4: .. tab-set:: .. tab-item:: Structure Double Dimension Array : typeof(expression) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Finding the type of an expression is easy. See below =================== ==================== ========================================================= Expression Type Description =================== ==================== ========================================================= a[0][0] struct XYZ * ``a[0][0]`` is a structure a[1][0] struct XYZ * ``a[1][0]`` is a structure a[2][0] struct XYZ * ``a[2][0]`` is a structure &a[0][0] struct XYZ * * ``&a[0][0]`` is address of a structure &a[1][0] struct XYZ * * ``&a[1][0]`` is address of a structure &a[2][0] struct XYZ * * ``&a[2][0]`` is address of a structure \*\*a struct XYZ * ``**a`` is structure. Same as a[0][0] \*(\*(a + 1) + 0) struct XYZ * ``*(*(a + 1) + 0)`` is a structure. Same as ``a[1][0]`` \*(\*(a + 2) + 0) struct XYZ * ``*(*(a + 2) + 0)`` is a structure. Same as ``a[2][0]`` a[0] struct XYZ * * ``a[0]`` is a single dimension structure array * Hence type is ``struct XYZ *`` a[1] struct XYZ * * ``a[1]`` is a single dimension structure array * Hence type is ``struct XYZ *`` a[2] struct XYZ * * ``a[2]`` is a single dimension structure array * Hence type is ``struct XYZ *`` &a[0] struct XYZ * * ``a[0]`` is a single dimension structure array * Hence ``typeof(&a[0])`` is ``struct XYZ (* )[4]`` &a[1] struct XYZ * * ``a[1]`` is a single dimension structure array * Hence ``typeof(&a[1])`` is ``struct XYZ (* )[4]`` &a[2] struct XYZ * * ``a[2]`` is a single dimension structure array * Hence ``typeof(&a[2])`` is ``struct XYZ (* )[4]`` \*a struct XYZ * ``*a`` is a single dimension structure array * Hence type is ``struct XYZ *`` \*(a + 1) struct XYZ * ``*(a + 1)`` is a single dimension structure array * Hence type is ``struct XYZ *`` \*(a + 2) struct XYZ * ``*(a + 2)`` is a single dimension structure array * Hence type is ``struct XYZ *`` a struct XYZ * * ``a`` is equal to ``&a[0]`` * Hence ``typeof(a)`` is ``struct XYZ (* )[4]`` a + 1 struct XYZ * * ``a + 1`` is equal to ``&a[1]`` * Hence ``typeof(a + 1)`` is ``struct XYZ (* )[4]`` a + 2 struct XYZ * * ``a + 2`` is equal to ``&a[2]`` * Hence ``typeof(a + 2)`` is ``struct XYZ (* )[4]`` &a struct XYZ (*)[3][4] * ``&a`` is address of complete array =================== ==================== ========================================================= .. _array_n_ptrs_struct_dd_array_ex_5: .. tab-set:: .. tab-item:: Structure Double Dimension Array : sizeof(expression) ========================== ========= =============================== sizeof(expression) size Description ========================== ========= =============================== sizeof(a[0][0]) 12 a[0][0] is a structure sizeof(a[1][0]) 12 a[1][0] is a structure sizeof(a[2][0]) 12 a[2][0] is a structure sizeof(&a[0][0]) 8 &a[0][0] is address / pointer sizeof(&a[1][0]) 8 &a[1][0] is address / pointer sizeof(&a[2][0]) 8 &a[2][0] is address / pointer sizeof(\*\*a) 12 \*\*a is a structure sizeof(\*(\*(a + 1) + 0)) 12 \*(\*(a + 1) + 0) is a structure sizeof(\*(\*(a + 2) + 0)) 12 \*(\*(a + 2) + 0) is a structure sizeof(a[0]) 48 a[0] is a single dimension array sizeof(a[1]) 48 a[1] is a single dimension array sizeof(a[2]) 48 a[2] is a single dimension array sizeof(&a[0]) 8 &a[0] is address / pointer sizeof(&a[1]) 8 &a[1] is address / pointer sizeof(&a[2]) 8 &a[2] is address / pointer sizeof(\*a) 48 \*a is a single dimension array sizeof(\*(a + 1)) 48 \*(a + 1) is a single dimension array sizeof(\*(a + 2)) 48 \*(a + 2) is a single dimension array sizeof(a) 144 a is a double dimension array sizeof(a + 1) 8 a + 1 is address / pointer sizeof(a + 2) 8 a + 2 is address / pointer sizeof(&a) 8 &a is address / pointer ========================== ========= =============================== * See the full program below .. code-block:: c #include struct XYZ { int x; int y; int z; }; int main(void) { struct XYZ a[3][4]; printf("sizeof(a[0][0]) = %d\n", (int) sizeof(a[0][0]) ); printf("sizeof(a[1][0]) = %d\n", (int) sizeof(a[1][0]) ); printf("sizeof(a[2][0]) = %d\n", (int) sizeof(a[2][0]) ); printf("sizeof(&a[0][0]) = %d\n", (int) sizeof(&a[0][0]) ); printf("sizeof(&a[1][0]) = %d\n", (int) sizeof(&a[1][1]) ); printf("sizeof(&a[2][0]) = %d\n", (int) sizeof(&a[2][0]) ); printf("sizeof(**a) = %d\n", (int) sizeof(**a)); printf("sizeof(*(*(a + 1) + 0)) = %d\n", (int) sizeof(*(*(a + 1) + 0)) ); printf("sizeof(*(*(a + 2) + 0)) = %d\n", (int) sizeof(*(*(a + 2) + 0)) ); printf("sizeof(a[0]) = %d\n", (int) sizeof(a[0]) ); printf("sizeof(a[1]) = %d\n", (int) sizeof(a[1]) ); printf("sizeof(a[2]) = %d\n", (int) sizeof(a[2]) ); printf("sizeof(&a[0]) = %d\n", (int) sizeof(&a[0]) ); printf("sizeof(&a[1]) = %d\n", (int) sizeof(&a[1]) ); printf("sizeof(&a[2]) = %d\n", (int) sizeof(&a[2]) ); printf("sizeof(*a) = %d\n", (int) sizeof(*a) ); printf("sizeof(*(a + 1)) = %d\n", (int) sizeof(*(a + 1)) ); printf("sizeof(*(a + 2)) = %d\n", (int) sizeof(*(a + 2)) ); printf("sizeof(a) = %d\n", (int) sizeof(a) ); printf("sizeof(a + 1) = %d\n", (int) sizeof(a + 1) ); printf("sizeof(a + 2) = %d\n", (int) sizeof(a + 2) ); printf("sizeof(&a) = %d\n", (int) sizeof(&a) ); return 0; } * Output is as below on 64 bit OS .. code-block:: c sizeof(a[0][0]) = 12 sizeof(a[1][0]) = 12 sizeof(a[2][0]) = 12 sizeof(&a[0][0]) = 8 sizeof(&a[1][0]) = 8 sizeof(&a[2][0]) = 8 sizeof(**a) = 12 sizeof(*(*(a + 1) + 0)) = 12 sizeof(*(*(a + 2) + 0)) = 12 sizeof(a[0]) = 48 sizeof(a[1]) = 48 sizeof(a[2]) = 48 sizeof(&a[0]) = 8 sizeof(&a[1]) = 8 sizeof(&a[2]) = 8 sizeof(*a) = 48 sizeof(*(a + 1)) = 48 sizeof(*(a + 2)) = 48 sizeof(a) = 144 sizeof(a + 1) = 8 sizeof(a + 2) = 8 sizeof(&a) = 8 .. _array_n_ptrs_struct_dd_array_ex_6: .. tab-set:: .. tab-item:: Structure Double 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(a[0][0]) void fun(struct XYZ x) {} * Call by Value fun(a[1][0]) void fun(struct XYZ x) {} * Call by Value fun(a[2][0]) void fun(struct XYZ x) {} * Call by Value fun(&a[0][0]) void fun(struct XYZ \*p) { } * Call by Reference fun(&a[1][0]) void fun(struct XYZ \*p) { } * Call by Reference fun(&a[2][0]) void fun(struct XYZ \*p) { } * Call by Reference fun(\*\*a) void fun(struct XYZ x) {} * Call by Value fun(\*(\*(a + 1) + 0)) void fun(struct XYZ x) {} * Call by Value fun(\*(\*(a + 2) + 0)) void fun(struct XYZ x) {} * Call by Value fun(a[0]) void fun(struct XYZ \*p) { } * Call by Reference fun(a[1]) void fun(struct XYZ \*p) { } * Call by Reference fun(a[2]) void fun(struct XYZ \*p) { } * Call by Reference fun(&a[0]) void fun(struct XYZ (\*p)[4]) { } * Call by Reference fun(&a[1]) void fun(struct XYZ (\*p)[4]) { } * Call by Reference fun(&a[2]) void fun(struct XYZ (\*p)[4]) { } * Call by Reference fun(\*a) void fun(struct XYZ \*p) { } * Call by Reference fun(\*(a + 1)) void fun(struct XYZ \*p) { } * Call by Reference fun(\*(a + 2)) void fun(struct XYZ \*p) { } * Call by Reference fun(a) void fun(struct XYZ (\*p)[4]) { } * Call by Reference fun(a + 1) void fun(struct XYZ (\*p)[4]) { } * Call by Reference fun(a + 2) void fun(struct XYZ (\*p)[4]) { } * Call by Reference fun(&a) void fun(struct XYZ (\*p)[3][4]) { } * Call by Reference ========================== ==================================== ========================================================================== .. _array_n_ptrs_struct_dd_array_ex_7: .. tab-set:: .. tab-item:: Structure Double Dimension Array : All Examples of Function Calls .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Read more about function calls and conventions of :doc:`../../funcs_n_ptrs/funcs_n_structs/struct_dd_array` .. card:: See Also * Other topics of character and functions * :doc:`./struct_sd_array` * :doc:`./struct_dd_array` * :doc:`./struct_td_array` * Current Module * :doc:`../array_n_ptrs` * Previous Module * :doc:`../../basic_ptr/basic_ptr` * Next Module * :doc:`../../malloc_ptr/malloc_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :doc:`../../malloc_ptr/malloc_ptr` * :doc:`../../typecasting_n_ptr/typecasting_n_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:`../../memcpy_ptr/memcpy_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`