Initialize a Struct in C

  1. Use Initializer List Style Notation to Initialize a Struct in C
  2. Use Assignment List Notation to Initialize a Struct in C
  3. Use Individual Assignment to Initialize a Struct in C

This article will introduce multiple methods about how to initialize a struct in C.

Use Initializer List Style Notation to Initialize a Struct in C

struct is probably the most important keyword for building complex data structures in C. It’s a built-in object that can store multiple heterogeneous elements called members.

Note that structures are defined with only the struct keyword, but in the following examples, we add typedef to create a new type name and make subsequent declarations more readable.

Once the structure is defined, we can declare a variable of this type and initialize it with list notation. This syntax is similar to the initializer list used in C++. In this case, we assign each member of the struct with an explicit assignment operator, but we can only specify values in the correct order, and that would be sufficient in modern versions of the language.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct Person{
    char firstname[40];
    char lastname[40];
    int age;
    bool alive;
} Person;

int main(void) {
    Person me = { .firstname = "John\0",
                  .lastname = "McCarthy\0",
                  .age = 24,
                  .alive = 1};

    printf("Name: %s\nLast Name: %s\nAge: %d\nAlive: ",
           me.firstname, me.lastname, me.age);
    me.alive ? printf("Yes\n") : printf("No\n");

    exit(EXIT_SUCCESS);
}

Output:

Name: John
Last Name: McCarthy
Age: 24
Alive: Yes

Use Assignment List Notation to Initialize a Struct in C

Alternatively, there might be a scenario when a declared struct is not initialized immediately and needs to be assigned values later in the program. In this case, we should use the initializer list-style syntax with an additional cast notation as a prefix. The casting to the type of the struct is the necessary step to compile the program.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct Person{
    char firstname[40];
    char lastname[40];
    int age;
    bool alive;
} Person;

int main(void) {
    Person me;

    me = (Person) { .firstname = "John\0",
            .lastname = "McCarthy\0",
            .age = 24,
            .alive = 1};

    printf("Name: %s\nLast Name: %s\nAge: %d\nAlive: ",
           me.firstname, me.lastname, me.age);
    me.alive ? printf("Yes\n") : printf("No\n");

    exit(EXIT_SUCCESS);
}

Output:

Name: John
Last Name: McCarthy
Age: 24
Alive: Yes

Use Individual Assignment to Initialize a Struct in C

Another method to initialize struct members is to declare a variable and then assign each member with its corresponding value separately. Note that char arrays can’t be assigned with string, so they need to be copied explicitly with additional functions like memcpy or memmove (see manual). You should always mind the length of the array shall not be less than the string being stored.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef struct Person{
    char firstname[40];
    char lastname[40];
    int age;
    bool alive;
} Person;

int main(void) {
    Person me2;

    memcpy(&me2.firstname, "Jane\0", 40);
    memcpy(&me2.lastname, "Delaney\0", 40);
    me2.age = 27;
    me2.alive = true;

    printf("Name: %s\nLast Name: %s\nAge: %d\nAlive: ",
           me2.firstname, me2.lastname, me2.age);
    me2.alive ? printf("Yes\n") : printf("No\n");

    exit(EXIT_SUCCESS);
}

Output:

Name: Jane
Last Name: Delaney
Age: 27
Alive: Yes

Related Article - C Struct

  • Allocate Struct Memory With malloc in C