0% found this document useful (0 votes)
12 views5 pages

Structures and Unions in C Pro

Structures and unions are user-defined data types in C that help organize complex data. Structures allow grouping of different data types under a single name, enabling easy access and manipulation, while unions share the same memory location among their members, allowing only one member to hold a value at a time. The document outlines their syntax, examples, advantages, and key differences, guiding when to use each type.

Uploaded by

06303648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Structures and Unions in C Pro

Structures and unions are user-defined data types in C that help organize complex data. Structures allow grouping of different data types under a single name, enabling easy access and manipulation, while unions share the same memory location among their members, allowing only one member to hold a value at a time. The document outlines their syntax, examples, advantages, and key differences, guiding when to use each type.

Uploaded by

06303648
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Structures and unions are user-defined data types in C that allow grouping of different data types under

a single name. They are essential for organizing complex data efficiently.

1. Structures in C

A structure (struct) is a collection of variables (of different types) grouped together under a single name.
It allows accessing and manipulating related data easily.

Syntax of Structure:

CopyEdit

struct structure_name {

data_type member1;

data_type member2;

...

};

Example of Structure:

CopyEdit

#include <stdio.h>

// Defining a structure

struct Student {

char name[50];

int age;

float marks;

};

int main() {

struct Student s1 = {"Alice", 20, 89.5};

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


printf("Age: %d\n", s1.age);

printf("Marks: %.2f\n", s1.marks);

return 0;

Accessing Structure Members:

 Use the dot operator (.) with the structure variable to access members.

 Example: s1.age to access the age of the student.

Pointer to Structure:

You can use pointers to access structure members using the arrow operator (->).

CopyEdit

struct Student *ptr = &s1;

printf("Name: %s", ptr->name);

Nested Structures:

A structure can contain another structure.

CopyEdit

struct Address {

char city[30];

int pin;

};

struct Student {

char name[50];

struct Address addr;

};

Array of Structures:

CopyEdit
struct Student students[2] = {

{"Bob", 19, 85.0},

{"Eve", 21, 92.0}

};

printf("Name of second student: %s", students[1].name);

Advantages of Structures:

 Groups related data together.

 Easier to manage and access complex data.

 Supports modular programming by defining data types once and reusing them.

2. Unions in C

A union is similar to a structure but with a crucial difference: all members share the same memory
location. This means only one member can store a value at any given time.

Syntax of Union:

CopyEdit

union union_name {

data_type member1;

data_type member2;

...

};

Example of Union:

CopyEdit

#include <stdio.h>

union Data {

int i;

float f;
char str[20];

};

int main() {

union Data data;

data.i = 10;

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

data.f = 3.14;

printf("f: %.2f\n", data.f); // Overwrites i

return 0;

Key Difference from Structures:

 Structures allocate memory for all members separately.

 Unions allocate shared memory, storing only one member at a time.

Memory Allocation Comparison:

CopyEdit

struct ExampleStruct {

int a; // 4 bytes

float b; // 4 bytes

char c; // 1 byte (padding may apply)

};

// Total size ≈ 12 bytes

union ExampleUnion {

int a;

float b;
char c;

};

// Total size = largest member (e.g. 4 bytes)

Advantages of Unions:

 Efficient memory usage when variables are mutually exclusive.

 Useful in embedded systems and low-memory applications.

Disadvantages of Unions:

 Can hold only one value at a time.

 May lead to data corruption if not used carefully.

Key Differences Between Structure and Union

Feature Structure Union

Memory Allocates memory for all members separately. Shares memory for all members.

Data Handling All members can hold values simultaneously. Only one member holds a value at a time.

Size Sum of sizes of all members. Size of the largest member.

Usage Used for storing different data elements. Used when variables share memory.

Example Use Complex records (student info). Efficient memory use (flags, status).

Conclusion

 Use structures when you need to store multiple data elements together.

 Use unions when you need efficient memory management and mutually exclusive values.

You might also like