C - structs and unions in C

1. Structures in C

A structure (struct) is a user-defined data type that groups variables of different types under one name.

Syntax:

struct Person {
    char name[50];
    int age;
    float height;
};

Usage:

struct Person p1 = {"Alice", 25, 5.4};

printf("%s is %d years old and %.1f ft tall\n", p1.name, p1.age, p1.height);

Key Points about Structures

  • Each member has its own memory location.

  • Size of a struct is sum of members’ sizes + padding (due to alignment).

  • You access members with the . operator, or -> if you have a pointer to a struct.

Example:

struct Person *ptr = &p1;
printf("%d", ptr->age); // same as (*ptr).age

2. Unions in C

A union is similar to a structure, but all members share the same memory location.

Syntax:

union Data {
    int i;
    float f;
    char str[20];
};

Usage:

union Data d;
d.i = 10;
printf("%d\n", d.i);

d.f = 220.5; // overwrites i
printf("%.1f\n", d.f);

Key Points about Unions

  • All members occupy the same memory block.

  • The size of a union = size of its largest member.

  • Only one member holds a valid value at a time.

  • Useful for memory-efficient storage when you need only one type active at a time.


3. Struct vs Union Memory Layout

Example:

struct Example {
    int i;
    float f;
};

union ExampleU {
    int i;
    float f;
};
  • Struct size = sizeof(int) + sizeof(float) (plus padding)

  • Union size = max(sizeof(int), sizeof(float))


4. When to Use

  • Struct → when you need all fields active at the same time (e.g., person info, records).

  • Union → when you need only one field active at a time and want to save memory (e.g., variant data types, hardware registers).


5. Struct and Union inside Each Other

You can combine them:

struct Value {
    int type; // 0=int, 1=float
    union {
        int i;
        float f;
    } data;
};

Example usage:

struct Value v;
v.type = 0;
v.data.i = 42;

 

If you want, I can draw a side-by-side memory diagram showing exactly how structs and unions store data — that makes the difference very obvious visually.
Do you want me to prepare that?


1. Structures in C

A structure (struct) is a user-defined data type that groups variables of different types under one name.

Syntax:

struct Person {
    char name[50];
    int age;
    float height;
};

Usage:

struct Person p1 = {"Alice", 25, 5.4};

printf("%s is %d years old and %.1f ft tall\n", p1.name, p1.age, p1.height);

Key Points about Structures

  • Each member has its own memory location.

  • Size of a struct is sum of members’ sizes + padding (due to alignment).

  • You access members with the . operator, or -> if you have a pointer to a struct.

Example:

struct Person *ptr = &p1;
printf("%d", ptr->age); // same as (*ptr).age

2. Unions in C

A union is similar to a structure, but all members share the same memory location.

Syntax:

union Data {
    int i;
    float f;
    char str[20];
};

Usage:

union Data d;
d.i = 10;
printf("%d\n", d.i);

d.f = 220.5; // overwrites i
printf("%.1f\n", d.f);

Key Points about Unions

  • All members occupy the same memory block.

  • The size of a union = size of its largest member.

  • Only one member holds a valid value at a time.

  • Useful for memory-efficient storage when you need only one type active at a time.


3. Struct vs Union Memory Layout

Example:

struct Example {
    int i;
    float f;
};

union ExampleU {
    int i;
    float f;
};
  • Struct size = sizeof(int) + sizeof(float) (plus padding)

  • Union size = max(sizeof(int), sizeof(float))


4. When to Use

  • Struct → when you need all fields active at the same time (e.g., person info, records).

  • Union → when you need only one field active at a time and want to save memory (e.g., variant data types, hardware registers).


5. Struct and Union inside Each Other

You can combine them:

struct Value {
    int type; // 0=int, 1=float
    union {
        int i;
        float f;
    } data;
};

Example usage:

struct Value v;
v.type = 0;
v.data.i = 42;