Module 9 of 13 · C Programming Fundamentals · Beginner

Structures & Unions

Duration: 50 min

Structures allow you to group multiple variables of different types into a single unit. Unions are similar but share memory space. These are fundamental for creating complex data types and organizing related data.

Structure Declaration & Initialization

#include <stdio.h>

// Define a structure
struct Student {
    int id;
    char name[50];
    float gpa;
};

int main() {
    // Create a structure variable
    struct Student s1;
    
    // Initialize members
    s1.id = 101;
    strcpy(s1.name, "Alice");
    s1.gpa = 3.8;
    
    // Access members
    printf("ID: %d\n", s1.id);
    printf("Name: %s\n", s1.name);
    printf("GPA: %.2f\n", s1.gpa);
    
    return 0;
}

Structure Initialization Methods

#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    // Method 1: Member by member
    struct Point p1;
    p1.x = 10;
    p1.y = 20;
    
    // Method 2: Initialization list
    struct Point p2 = {30, 40};
    
    // Method 3: Designated initializers (C99)
    struct Point p3 = {.x = 50, .y = 60};
    
    printf("p1: (%d, %d)\n", p1.x, p1.y);
    printf("p2: (%d, %d)\n", p2.x, p2.y);
    printf("p3: (%d, %d)\n", p3.x, p3.y);
    
    return 0;
}

typedef with Structures

#include <stdio.h>

// Define and create alias in one step
typedef struct {
    int id;
    char name[50];
    float salary;
} Employee;

int main() {
    Employee emp1 = {1, "Bob", 50000.0};
    Employee emp2 = {2, "Carol", 60000.0};
    
    printf("Employee 1: %s, $%.2f\n", emp1.name, emp1.salary);
    printf("Employee 2: %s, $%.2f\n", emp2.name, emp2.salary);
    
    return 0;
}

Pointers to Structures

#include <stdio.h>

struct Car {
    char brand[30];
    int year;
    float price;
};

int main() {
    struct Car car = {"Toyota", 2020, 25000.0};
    struct Car *ptr = &car;
    
    // Access members using pointer
    printf("Brand: %s\n", ptr->brand);
    printf("Year: %d\n", ptr->year);
    printf("Price: $%.2f\n", ptr->price);
    
    // Alternative: (*ptr).brand
    printf("Alternative: %s\n", (*ptr).brand);
    
    return 0;
}

Nested Structures

#include <stdio.h>

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

struct Person {
    char name[50];
    int age;
    struct Address addr;
};

int main() {
    struct Person p = {
        "John",
        30,
        {"New York", "USA"}
    };
    
    printf("Name: %s\n", p.name);
    printf("City: %s\n", p.addr.city);
    printf("Country: %s\n", p.addr.country);
    
    return 0;
}

Unions

#include <stdio.h>

// Union: members share the same memory
union Data {
    int i;
    float f;
    char c;
};

int main() {
    union Data data;
    
    printf("Size of union: %lu bytes\n", sizeof(union Data));
    
    data.i = 10;
    printf("data.i: %d\n", data.i);
    
    // Assigning to f overwrites i
    data.f = 3.14;
    printf("data.f: %.2f\n", data.f);
    printf("data.i: %d\n", data.i);  // Changed!
    
    return 0;
}

Array of Structures

#include <stdio.h>

struct Book {
    char title[50];
    char author[50];
    float price;
};

int main() {
    struct Book books[3] = {
        {"C Programming", "Kernighan", 45.99},
        {"Data Structures", "Cormen", 89.99},
        {"Algorithms", "Sedgewick", 75.99}
    };
    
    for (int i = 0; i < 3; i++) {
        printf("%s by %s: $%.2f\n", 
               books[i].title, 
               books[i].author, 
               books[i].price);
    }
    
    return 0;
}

Practical Example: Rectangle Structure

#include <stdio.h>

typedef struct {
    float width;
    float height;
} Rectangle;

float area(Rectangle r) {
    return r.width * r.height;
}

float perimeter(Rectangle r) {
    return 2 * (r.width + r.height);
}

int main() {
    Rectangle rect = {5.0, 3.0};
    
    printf("Area: %.2f\n", area(rect));
    printf("Perimeter: %.2f\n", perimeter(rect));
    
    return 0;
}

Quiz 1: Structure Definition

❓ What is a structure in C?

Quiz 2: Accessing Structure Members

❓ How do you access a member of a structure variable?

Quiz 3: Pointer to Structure

❓ How do you access a member through a pointer to a structure?

Quiz 4: Union vs Structure

❓ What is the main difference between a union and a structure?

Quiz 5: typedef

❓ What does typedef do?

← Previous Continue interactively → Next →

Related Courses