Defining structures and unions.

Lesson 6.1: Defining Structures and Unions


In this lesson, you'll learn about two powerful tools in C for organizing data: structures and unions. While they have similar syntax, they serve different purposes.


Structures (struct)


What are they?


Structures are user-defined data types that allow you to group together related variables of different data types under a single name.

They provide a way to create complex data types that can represent real-world entities (e.g., a person, a car, a book).

Why use structures?


Organization: Structures improve code readability and maintainability by grouping related data items.

Modularity: Structures can be passed to functions as a single unit, making your code more organized.

Syntax:


C

struct structName {

    dataType member1;

    dataType member2;

    // ... more members

};


Example:

C

struct Student {

    char name[50];

    int age;

    float gpa;

};


Unions (union)


What are they?


Unions are similar to structures, but all members share the same memory location.

This means that a union can only hold the value of one member at a time.

Why use unions?


Memory Conservation: Unions are useful when you need to store different types of data in the same memory location, saving memory.

Type Punning: Unions can be used for type punning, which involves interpreting the same memory location as different data types. (Use with caution, as it can lead to undefined behavior.)

Syntax:


C

union unionName {

    dataType member1;

    dataType member2;

    // ... more members

};

Use code with caution.

content_copy

Example:

C

union Data {

    int i;

    float f;

    char str[20];

};

Differences between Structures and Unions


Feature Structure (struct) Union (union)

Memory Allocation Allocates separate memory for each member. Allocates memory for the largest member, and all members share this memory location.

Size The size of a structure is the sum of the sizes of its members (plus potential padding for alignment). The size of a union is the size of its largest member.

Access You can access and modify all members of a structure at any time. You can only access and modify one member of a union at a time. The value of other members becomes undefined when you modify a different member.

Use Cases Representing complex data types (e.g., employee records, geometric shapes, points). Memory conservation, type punning (interpreting the same memory as different types), implementing variant records (data structures that can hold different types of data but only one at a time).



Example: Using Structures and Unions


C

#include <stdio.h>


struct Point {

    int x;

    int y;

};


union Number {

    int i;

    double d;

};


int main() {

    struct Point p1 = {5, 10};  // Initialize a Point structure

    union Number num;           // Declare a Number union


    num.i = 123;                // Store an integer in the union

    printf("num.i: %d\n", num.i); 

    num.d = 3.14;              // Store a double in the union (overwrites the integer value)

    printf("num.d: %f\n", num.d); 


    return 0;

}

Let me know if you'd like any clarification or want to see more advanced use cases for structures and unions!

Course Syllabus