Working with Structures and Unions in C

Markdown

View as Markdown

Structures and unions are user-defined data types in C that allow programmers to group multiple variables under a single name. They are widely used to represent real-world entities such as students, employees, products, and customer records.

Although structures and unions appear similar, they differ significantly in how they store data and use memory. Understanding these differences helps developers choose the appropriate data structure for different programming scenarios.

Learning Objectives

  • Understand structures and unions

  • Create and initialize structure variables

  • Access structure members

  • Work with arrays of structures

  • Create nested structures

  • Understand how unions manage memory

  • Compare structures and unions

What is a Structure?

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

Example:

struct Student

{

    int id;

    char name[30];

    float marks;

};

 

Creating Structure Variables

After defining a structure, you can create variables of that type.

Example:

#include <stdio.h>

struct Student

{

    int id;

    char name[30];

    float marks;

};

int main()

{

    struct Student student1 = {101, "John", 89.5};

    printf("ID: %d\n", student1.id);

    printf("Name: %s\n", student1.name);

    printf("Marks: %.1f\n", student1.marks);

    return 0;

}

Output

ID: 101

Name: John

Marks: 89.5

Accessing Structure Members

Members of a structure are accessed using the dot (.) operator.

Example:

student1.id

student1.name

student1.marks

 

Arrays of Structures

An array of structures stores multiple records of the same type.

Example:

#include <stdio.h>

struct Student

{

    int id;

    char name[20];

};

int main()

{

    struct Student students[2] =

    {

        {101,"John"},

        {102,"Priya"}

    };

    printf("%s\n", students[0].name);

    printf("%s\n", students[1].name);

    return 0;

}

Output

John

Priya

Nested Structures

Structures can contain other structures as members.

Example:

#include <stdio.h>

struct Address

{

    char city[20];

};

struct Student

{

    int id;

    struct Address address;

};

int main()

{

    struct Student student = {101, {"Doha"}};

    printf("%s", student.address.city);

    return 0;

}

Output

Doha

What is a Union?

A union is similar to a structure but stores all members in the same memory location.

Only one member can hold a valid value at a time.

Example:

union Data

{

    int id;

    float marks;

    char grade;

};

Using a Union

Example:

#include <stdio.h>

union Data

{

    int id;

    float marks;

};

int main()

{

    union Data data;

    data.id = 101;

    printf("%d\n", data.id);

    data.marks = 89.5;

    printf("%.1f\n", data.marks);

    return 0;

}

Output

101

89.5

Structure vs Union

Structure

Union

Separate memory for each member

Shared memory for all members

All members can store values simultaneously

Only one member contains a valid value at a time

Larger memory usage

Lower memory usage

Best for storing complete records

Best for memory optimization

 

Advantages of Structures

  • Organize related data

  • Improve readability

  • Simplify record management

  • Support complex data models

  • Widely used in databases and file handling

Advantages of Unions

  • Reduce memory usage

  • Useful in embedded systems

  • Efficient for hardware programming

  • Suitable when only one value is needed at a time

Common Mistakes

  • Confusing structures with unions

  • Accessing uninitialized members

  • Expecting all union members to retain values simultaneously

  • Using incorrect member names

Best Practices

  • Use structures for storing complete records.

  • Use unions when memory optimization is important.

  • Choose meaningful member names.

  • Initialize structures before use.

  • Keep nested structures simple and organized.

 

 

 

 

 

 

 

Was this article helpful?

Still need help?

Contact us