Structure in C

Structure in C allows us to create a complex data type. This new type can hold multiple values of different types. If you are familiar with enum in C, the syntax to define a structure will be easy for you.

Syntax



We create structures with the keyword struct. Normally we create them in a separate header file. Another option is to define them in our .c files, outside of any function.

struct [Tag]
{
    [Member definition];
    [Member definition];
    ...
    [Member definition];
} [List of variables to be created];
  • The keyword struct tells the compiler that we are creating a structure
  • The Tag is the name of the structure type. We can use it later to create variables. We call these variables instances. The Tag is optional.
  • Inside the body, we define the members. We can define as many as we need. Their type could be any valid C type, including other structures, unions etc.
  • After the body we have the option to create a list of variables of the current type. To do this, we just write their names and separate them with a comma.
  • Don’t forget the semicolon in the end! It is mandatory.

Example: Defining a structure in C

employee.h

struct Employee
{
    char name[30];
    char address[50];
    int age;
};

Creating instances

There are several ways to create variables of the struct type that you created. The first way is to list the variable names after the structure definition like this:

struct Employee
{
    char name[30];
    char address[50];
    int age;
} employee1, employee 2, employee 3;

Now you can work with the three employee variables.

I like to separate the types and the variables, so I prefer the second way. After you define the structure, use its Tag and the keyword struct to create a variable:

    struct Employee worker1;

Also, you can declare a pointer:

    struct Employee* worker2;

Accessing members

To access a member of a structure in C we use the dot ‘.’ and the arrow ‘->’ operators.

  • We use the dot operator when we work with an instance:
struct Employee worker1;
worker1.age = 20;
  • We use the arrow operator when we work with a pointer to an instance:
struct Employee* worker2;
worker2->age = 20;

Creating a complex structure in C

A complex structure is such that contains at least one member of complex type – for instance another structure. Let’s take the address member from above. Instead of keeping it in a string field, we can define it like this:

struct Employee
{
    struct Address
    {
        char country[30];
        char city[30];
        char street[30];
        char addressField[30];
    } address;
    char name[30];
    int age;
};

or like this:

address.h

struct Address
{
    char country[30];
    char city[30];
    char street[30];
    char addressField[30];
};

employee.h

struct Employee
{
    struct Address address;
    char name[30];
    int age;
};

Here is how the information in this complex structure looks after I input and output it:

Input/Output complex structure in C

Padding

In C, by default the data in a structure is automatically padded. The padding is adding empty bytes in the memory in order to align the data. This is done for two reasons:

  • For faster data access
  • On many processors data alignment is mandatory.

On most processor architectures data alignment is mandatory - for instance on processors with RISC architectures (ARM, MIPS…). Unaligned data is allowed on processors with CISC architecture (almost all desktop computers x86, x64..).

Here is how we define an example structure in C:

struct A
{
    char symbol;
    int number;
    short id;
};  

And here is how it really looks in the memory:

    struct A
    {
        char symbol;             // 1 byte in memory
        char CHAR_PADDING[3];    // 3 bytes padding    
        int number;              // 4 bytes - no padding needed
        short id;                // 2 bytes in memory
        char SHORT_PADDING[2];   // 2 bytes padding 
    };

So the result of sizeof(struct A) is 12 Bytes.

Structure in C - padding in memory

We can optimize our structures, by sorting their members by size. This results in more efficient placement in the memory and less padding bytes. For instance, we can optimize the above definition like this:

struct A
{
    int number;
    short id;
    char symbol;
};

Now only 1 byte padding is necessary at the end of the definition. In the memory it looks like this:

struct A
{
    int number;                // 4 bytes - no padding needed  
    short id;                  // 2 bytes in memory
    char symbol;               // 1 byte in memory
    char PADDING[1];           // 1 byte padding
};

Now sizeof(struct A) returns 8 Bytes.

Structure in C - optimized padding

Examples source download

After most articles I provide you with the source of the examples. This time is no different, but I really urge you to look at them. You can download the zip, containing the source and header files from here: structure-in-c-examples.zip

See also:

Previous keyword: static

Next keyword: switch

   Search this site: