C Programming

Unit 9: Structure and Union

Structures, array of structures, passing to functions, pointers to structures, nested structures, and unions.

Introduction

A structure is a user-defined data type that groups variables of different types under a single name. Each variable inside a structure is called a member.

Syntax:

struct structure_name {
    data_type member1;
    data_type member2;
    /* ... */
};

Example:

#include <stdio.h>

struct Student {
    int   roll;
    char  name[30];
    float marks;
};

int main(void)
{
    struct Student s1 = {1, "Ram", 85.5f};
    printf("Roll : %d\n",   s1.roll);
    printf("Name : %s\n",   s1.name);
    printf("Marks: %.2f\n", s1.marks);
    return 0;
}

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

Array of structure

An array of structure is used to store multiple records of the same type.

#include <stdio.h>

struct Student {
    int   roll;
    char  name[20];
    float marks;
};

int main(void)
{
    struct Student s[3] = {
        {1, "Ram",  85.5f},
        {2, "Sita", 90.0f},
        {3, "Hari", 78.0f}
    };

    for (int i = 0; i < 3; i++)
        printf("%d %s %.2f\n", s[i].roll, s[i].name, s[i].marks);
    return 0;
}

Passing structure to function

A structure can be passed to a function either by value or by reference.

#include <stdio.h>

struct Student {
    int   roll;
    float marks;
};

void display(struct Student s)
{
    printf("Roll: %d, Marks: %.2f\n", s.roll, s.marks);
}

int main(void)
{
    struct Student s1 = {1, 85.5f};
    display(s1);
    return 0;
}

Passing array of structure to function

#include <stdio.h>

struct Student {
    int   roll;
    float marks;
};

void show(struct Student s[], int n)
{
    for (int i = 0; i < n; i++)
        printf("Roll: %d, Marks: %.2f\n", s[i].roll, s[i].marks);
}

int main(void)
{
    struct Student s[2] = {{1, 85.0f}, {2, 90.5f}};
    show(s, 2);
    return 0;
}

Pointer to structure

A pointer to a structure stores the address of a structure variable. Members are accessed using the arrow (->) operator.

#include <stdio.h>

struct Student {
    int   roll;
    float marks;
};

int main(void)
{
    struct Student s1 = {1, 85.5f};
    struct Student *p  = &s1;

    printf("Roll : %d\n",   p->roll);
    printf("Marks: %.2f\n", p->marks);
    return 0;
}

Structure within structure (Nested structure)

A structure can be defined as a member of another structure.

#include <stdio.h>

struct Date {
    int day, month, year;
};

struct Student {
    int   roll;
    char  name[20];
    struct Date dob;
};

int main(void)
{
    struct Student s = {1, "Ram", {15, 6, 2005}};

    printf("Roll  : %d\n", s.roll);
    printf("Name  : %s\n", s.name);
    printf("DOB   : %d/%d/%d\n", s.dob.day, s.dob.month, s.dob.year);
    return 0;
}

Union

A union is a user-defined data type similar to a structure, but all its members share the same memory location. Only one member can hold a value at any given time.

Syntax:

union union_name {
    data_type member1;
    data_type member2;
    /* ... */
};

Example:

#include <stdio.h>

union Data {
    int   i;
    float f;
    char  ch;
};

int main(void)
{
    union Data d;

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

    d.f = 3.14f;
    printf("f  = %.2f\n", d.f);

    d.ch = 'A';
    printf("ch = %c\n", d.ch);
    return 0;
}

Key difference between structure and union:

  • A structure allocates separate memory for each member.
  • A union allocates memory equal to the size of its largest member, and all members share this memory.