0% found this document useful (0 votes)
1 views22 pages

Module 5

Uploaded by

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

Module 5

Uploaded by

bsnana67
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Structures, Unions, Enumerations, and

typedef
MODULE 5
What is a Structure?
A structure is a user-defined data type that lets you group different types of
data under one name.

Why use structures?


If you want to store related information together (like a student’s name, roll
number, and marks), structures are perfect.
Syntax:-
struct structure_name {
data_type member1;
data_type member2;
...
};
Example:-
#include <stdio.h>
struct Student {
int roll;
float marks;
};
int main() {
struct Student s1;
[Link] = 101;
[Link] = 92.5;
printf("Roll: %d\n", [Link]);
printf("Marks: %.2f\n", [Link]);
return 0;
}
Arrays of Structures
What is an Array of Structures?
An array of structures stores multiple structure variables of the
same type.
When is it useful?
When you need to store data of many objects, like multiple students.
Ex:- #include <stdio.h>
struct Student {
int roll;
float marks;
};

int main() {
struct Student s[3]; // Array of 3 students

for(int i = 0; i < 3; i++) {


printf("Enter roll and marks of student %d: ", i + 1);
scanf("%d %f", &s[i].roll, &s[i].marks);
}

for(int i = 0; i < 3; i++) {


printf("Student %d -> Roll: %d, Marks: %.2f\n",
i + 1, s[i].roll, s[i].marks);
}

return 0;
}
#include <stdio.h>

struct Student {
int roll;
Passing Structure to Functions float marks;
};
Method 1: Passing Structure
by Value void display(struct Student s) {
A copy of the structure is printf("Roll: %d\n", [Link]);
printf("Marks: %.2f\n", [Link]);
passed.
}
Changes inside the function do
not affect the original. int main() {
struct Student s1 = {101, 85.5};

display(s1);

return 0;
}
#include <stdio.h>
Method 2: Passing
Structure by Reference struct Student {
(using pointer) int roll;
float marks;
The function gets the };
address, so changes
affect the original void updateMarks(struct Student *s) {
structure. s->marks = 95.0;
}
int main() {
struct Student s1 = {101, 80.0};

updateMarks(&s1);

printf("Updated Marks: %.2f\n", [Link]);

return 0;
}
Example:-
Structure Pointers
#include <stdio.h>
What is a Structure Pointer?
A pointer that stores the address struct Student {
of a structure. int roll;
Syntax:- float marks;
struct Student *ptr; };

int main() {
Accessing Members Using struct Student s1 = {101, 90.5};
Pointer
struct Student *ptr = &s1;
Use the arrow operator (->)
instead of dot.
printf("Roll: %d\n", ptr->roll);
printf("Marks: %.2f\n", ptr->marks);

return 0;
}
Arrays and Structures within Structures
Example:-
Arrays within Structures #include <stdio.h>

struct Student {
When one or more members of a int roll;
structure are arrays, it is called char name[20]; // array inside structure
array within structure. float marks;
};
Used when:
• A student has multiple subjects int main() {
struct Student s = {1, "Sireesha", 85.5};
• An employee has multiple
allowances printf("Roll: %d\n", [Link]);
printf("Name: %s\n", [Link]);
• A person has multiple phone printf("Marks: %.2f\n", [Link]);
numbers
return 0;
}
Integer Array inside Structure

#include <stdio.h> printf("Roll: %d\n", [Link]);


printf("Marks:\n");
struct Student {
for(int i = 0; i < 3; i++)
int roll; printf("%d ", [Link][i]);
int marks[3]; // array of marks
}; return 0;
}

int main() {
struct Student s = {1, {80, 85,
90}};
Structures within Structures (Nested Structures)
When a structure is declared inside another structure, it is called
a structure within structure (nested structure).
Used when:
• Student has address
• Employee has date of joining
• Bank account has customer details
int main() {
#include <stdio.h> struct Student s = {1, "Sireesha", {"Mysuru",
struct Address { 570001}};
char city[20];
printf("Roll: %d\n", [Link]);
int pincode; printf("Name: %s\n", [Link]);
}; printf("City: %s\n", [Link]);
struct Student { printf("Pincode: %d\n", [Link]);
int roll;
return 0;
char name[20]; }
struct Address addr; //
structure inside structure
};
Array + Structure within Structure
#include <stdio.h>

struct Marks { printf("Roll: %d\n", [Link]);


int sub[3]; // array inside structure printf("Name: %s\n",
}; [Link]);
printf("Marks:\n");
struct Student {
int roll; for(int i = 0; i < 3; i++)
char name[20]; printf("%d ",
struct Marks m; // structure inside [Link][i]);
structure
}; return 0;
}
int main() {
struct Student s = {1, "Sireesha", {{85,
90, 88}}};
Real-Time Applications
• Student management systems
• Employee payroll systems
• Banking applications
• Hospital records
• IoT sensor data storage
Unions
- A union is a user-defined data type similar to a structure, but all
members share the same memory location.
- At any time, only one member can store a value.

Syntax:-

union UnionName {
data_type member1;
data_type member2;
};
#include <stdio.h>
union Data {
int i;
float f; d.f = 3.14;
char c; printf("Float: %.2f\n", d.f);
};
return 0;
int main() { }
union Data d;
d.i = 10;
printf("Integer: %d\n", d.i);
Difference: Structure vs Union

Structure Union

Separate memory for each member Shared memory

More memory usage Less memory usage

All members usable at once Only one member at a time


Using sizeof to Ensure Portability
The sizeof operator returns the number of bytes required by a data
type or variable.
It helps make programs portable across different systems (32-bit,
64-bit).
Why Portability
• Size of int, long, pointer may vary by compiler/system
• Hard-coding sizes can cause errors
Syntax:
sizeof(data_type)
sizeof(variable)
#include <stdio.h>

int main() {
int a;
printf("Size of int: %lu bytes\n", sizeof(a));
printf("Size of float: %lu bytes\n", sizeof(float));
return 0;
} Portable Memory Allocation

int *p = malloc(10 * sizeof(int));


typedef in C
The typedef keyword is used to create a new name (alias) for an
existing data type.

Purpose
• Improves code readability
• Simplifies complex declarations
• Enhances maintainability
Syntax:-
typedef existing_type new_name;
1. Basic:

typedef unsigned int uint; 3. Pointer Typedef

uint a = 10; typedef int* IntPtr;

IntPtr p;
2. Structure with typedef

typedef struct {
int id;
float marks;
} Student;

Student s1;
Overview

Topic Key Idea


Shared memory, one
Union
member at a time
Finds memory size, ensures
sizeof
portability
typedef Creates alias for data types

You might also like