Basics of Structure Double Dimension Array

In this section, you are going to learn

What are the basic properties of a structure double dimension array ?

What are the different expressions of double dimension array ?

What are synonymous expresions of double dimension array ?

How to find sizeof() of expressions of double dimension array ?

struct XYZ array_name[Row][Column];

Consider a structure double dimension array

struct XYZ {
        int x;
        int y;
        int z;
};

struct XYZ a[3][4];

Let us answer few basic questions in this array

How many structures can be stored in this array ?

How many bytes are there in this array ?

What is the sizeof the array ?

How many single dimension arrays are present in this double dimension array ?

What are the names of single dimension arrays in this double dimension array ?

How do you represent the first structure ?

How do you represent the last structure ?

How do you initialise the array at the time of declaration ?

Let us now explore basic examples of double dimension array !

  • Step 1 : Define a Double Dimension Array

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

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

#include <stdio.h>

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

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

Let us now explore double dimension array in depth !

Below are the properties

  • Expressions

  • Synonyms

  • typeof(expression)

  • sizeof(expression)

  • fun(expression)

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

Synonyms : Which can be used interchangeably !

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

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

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

#include <stdio.h>

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

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

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

Read more about function calls and conventions of Functions and Structure Double Dimension Array