cstructaddressing

Does 1-dimension variable with type struct become 2-dimension variable in C


I have a struct type and a variable with defined like this:

typedef struct test
{
    uint8_t            a;        
    uint8_t            b;            
    uint8_t            c;            
} test_type;


test_type x[2];

Does this make this variable to behave like a 2-dimension variable? I mean that when I call x[0], will it return the address of the 1st variable which have struct test_type defined above?


Solution

  • The memory layout of the struct will be the same if it were a two-dimensional array i.e. a,b,c will be stored after one another in memory however you need to take care that the compiler does not insert padding between them. A real two-dimensional doesn't have that problem.

    The compiler usually aligns structure members to natural address boundaries so depending on what size your variables have in the struct, it may insert extra bytes between the fields in the structure.