Basics of Structure Single Dimension Array

In this section, you are going to learn

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

What are the different expressions of single dimension array ?

What are synonymous expresions of single dimension array ?

How to find sizeof() of expressions of single dimenstion array ?

struct XYZ array_name[Column];

Consider a Structure Single Dimension Array

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

struct XYZ a[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 bits can be stored in this array ?

How do you initialise single dimension array during declaration ?

Let us now explore basic examples of single dimension array !

  • Step 1 : Define a Single Dimension Array

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

struct XYZ a[4] = {
        {.x = 11, .y = 22, .z = 33},
        {.x = 44, .y = 55, .z = 66},
        {.x = 77, .y = 88, .z = 99},
        {.x = 12, .y = 34, .z = 56},
};
  • Step 2 : Access and Print individual structures

for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
        printf("a[%d].x = %d ", i, a[i].x);
        printf("a[%d].y = %d ", i, a[i].y);
        printf("a[%d].z = %d ", i, a[i].z);
        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[4] = {
                {.x = 11, .y = 22, .z = 33},
                {.x = 44, .y = 55, .z = 66},
                {.x = 77, .y = 88, .z = 99},
                {.x = 12, .y = 34, .z = 56},
        };

        for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
        {
                printf("a[%d].x = %d ", i, a[i].x);
                printf("a[%d].y = %d ", i, a[i].y);
                printf("a[%d].z = %d ", i, a[i].z);
                printf("\n");
        }

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

a[0].x = 11 a[0].y = 22 a[0].z = 33

a[1].x = 44 a[1].y = 55 a[1].z = 66

a[2].x = 77 a[2].y = 88 a[2].z = 99

a[3].x = 12 a[3].y = 34 a[3].z = 56

Let us now explore Double Dimension Array in depth !

Consider a structure single dimension array

struct XYZ a[10];

Then 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]

  • a[0] is first structure in the array

a[1]

  • a[1] is second structure in the array

a[9]

  • a[9] is tenth structure in the array

&a[0]

  • &a[0] is address of first structure

&a[1]

  • &a[1] is address of second structure

&a[9]

  • &a[9] is address of tenth structure

*a

  • *a is first structure in the array

*(a + 1)

  • *(a + 1) is second structure in the array

*(a + 9)

  • *(a + 9) is tenth structure in the array

a

  • a is address of first structure

  • a is an array of ten structures

a + 1

  • a + 1 is address of second structure

  • a is an array of nine structures

a + 9

  • a + 9 is address of tenth structure

  • a + 9 is an array of one structure

&a

  • &a is the address of array of structures

Synonyms : Which can be used interchangeably !

Programmatically few expressions are one and the same ! Let us learn them

Expression

Synonyms

a[0]

*a

a[1]

*(a + 1)

a[9]

*(a + 9)

&a[0]

a + 0

a

&a[1]

a + 1

&a[9]

a + 9

*a

a[0]

*(a + 1)

a[1]

*(a + 9)

a[9]

a

&a[0]

a + 1

&a[1]

a + 9

&a[9]

&a

&a

Finding the type of an expression is easy. See below

Expression

Type

Description

a[0]

struct XYZ

  • a[0] is a structure

  • Hence type is struct XYZ

a[1]

struct XYZ

  • a[1] is a structure

  • Hence type is struct XYZ

a[9]

struct XYZ

  • a[9] is a structure

  • Hence type is struct XYZ

&a[0]

struct XYZ *

  • a[0] is a structure

  • typeof(a[0]) is struct XYZ

  • Hence typeof(&a[0]) is struct XYZ *

&a[1]

struct XYZ *

  • a[1] is a structure

  • typeof(a[1]) is struct XYZ

  • Hence typeof(&a[1]) is struct XYZ *

&a[9]

struct XYZ *

  • a[9] is a structure

  • typeof(a[9]) is struct XYZ

  • Hence typeof(&a[9]) is struct XYZ *

*a

struct XYZ

  • *a is a structure

  • Hence type is struct XYZ

*(a + 1)

struct XYZ

  • *(a + 1) is a structure

  • Hence type is struct XYZ

*(a + 9)

struct XYZ

  • *(a + 9) is a structure

  • Hence type is struct XYZ

a

struct XYZ *

  • a is equal to &a[0]

  • Hence typeof(a) is struct XYZ *

a + 1

struct XYZ *

  • a + 1 is equal to &a[1]

  • Hence typeof(a + 1) is struct XYZ *

a + 9

struct XYZ *

  • a + 9 is equal to &a[9]

  • Hence typeof(a + 9) is struct XYZ *

&a

struct XYZ (* )[10]

  • &a is address of complete array

sizeof(expression)

size

Description

sizeof(a[0])

12 Bytes

a[0] is a structure

sizeof(a[1])

12 Bytes

a[1] is a structure

sizeof(a[9])

12 Bytes

a[9] is a struct XYZcter

sizeof(&a[0])

8 Bytes

&a[0] is address / pointer

sizeof(&a[1])

8 Bytes

&a[1] is address / pointer

sizeof(&a[9])

8 Bytes

&a[9] is address / pointer

sizeof(*a)

12 Bytes

*a is a structure

sizeof(*(a + 1) )

12 Bytes

*(a + 1) is a structure

sizeof(*(a + 9) )

12 Bytes

*(a + 9) is a structure

sizeof(a)

120 Bytes

a is an array of 10 structures

sizeof(a + 1)

8 Bytes

a + 1 is address / pointer

sizeof(a + 9)

8 Bytes

a + 9 is address / pointer

sizeof(&a)

8 Bytes

&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[10];

        printf("sizeof(a[0])            = %d\t\n", (int) sizeof(a[0]) );
        printf("sizeof(a[1])            = %d\t\n", (int) sizeof(a[1]) );
        printf("sizeof(a[9])            = %d\t\n", (int) sizeof(a[9]) );
        printf("sizeof(&a[0])           = %d\t\n", (int) sizeof(&a[0]) );
        printf("sizeof(&a[1])           = %d\t\n", (int) sizeof(&a[1]) );
        printf("sizeof(&a[9])           = %d\t\n", (int) sizeof(&a[9]) );
        printf("sizeof(*a)              = %d\t\n", (int) sizeof(*a) );
        printf("sizeof( *(a + 1) )      = %d\t\n", (int) sizeof( *(a + 1) ) );
        printf("sizeof( *(a + 9) )      = %d\t\n", (int) sizeof( *(a + 9) ) );
        printf("sizeof(a)               = %d\t\n", (int) sizeof(a) );
        printf("sizeof(a + 1)           = %d\t\n", (int) sizeof(a + 1) );
        printf("sizeof(a + 9)           = %d\t\n", (int) sizeof(a + 9) );
        printf("sizeof(&a)              = %d\t\n", (int) sizeof(&a) );

        return 0;
}
  • Output is as below on 64 bit OS

sizeof(a[0])            = 12
sizeof(a[1])            = 12
sizeof(a[9])            = 12
sizeof(&a[0])           = 8
sizeof(&a[1])           = 8
sizeof(&a[9])           = 8
sizeof(*a)              = 12
sizeof( *(a + 1) )      = 12
sizeof( *(a + 9) )      = 12
sizeof(a)               = 120
sizeof(a + 1)           = 8
sizeof(a + 9)           = 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])

void fun(struct XYZ x) { }

  • Call by Value

fun(a[1])

void fun(struct XYZ x) { }

  • Call by Value

fun(a[9])

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[9])

void fun(struct XYZ *p) { }

  • Call by Reference

fun(*a)

void fun(struct XYZ x) { }

  • Call by Value

fun(*(a + 1))

void fun(struct XYZ x) { }

  • Call by Value

fun(*(a + 9))

void fun(struct XYZ x) { }

  • Call by Value

fun(a)

void fun(struct XYZ *p) { }

  • Call by Reference

fun(a + 1)

void fun(struct XYZ *p) { }

  • Call by Reference

fun(a + 9)

void fun(struct XYZ *p) { }

  • Call by Reference

fun(&a)

void fun(struct XYZ (*p) [10] ) { }

  • Call by Reference

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