0% found this document useful (0 votes)
13 views

module 4

Uploaded by

mytreshgowd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

module 4

Uploaded by

mytreshgowd
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Module – 4

Structures and
Unions in C
What will you learn?
 Structures
 Unions
 Structures versus Unions
WHY STRUCTURES?
Structures are used to group different types of data
together under a single name. (e.g., representing a person's
information such as age, name, and salary).

 Organizing Complex Data

 Improving Code Readability and Maintainability

 Memory Management

 Custom Data Types

 Facilitating Data Abstraction

 Efficient Passing of Data


STRUCTUR
ES
“A structure is defined as a collection of dissimilar data types under one single
name”.
STRUCTURE TYPE
DECLARATION
Syntax: - Example: -

struct book_bank
struct tag_name
{
{
char title[25];
data type member 1;
char author[20];
data type member 2;
int pages;
data type member 3;
float price;
...
};
data type member n;
};
DECLARING STRUCTURE
VARIABLES
After defining the structure, we declare variables of the
structure data type. A structure variable declaration is similar to
that of an ordinary variable declaration.

Example: -

struct book_bank book1, book2, book3;

Here book1, book2, book3 are the structure variables of


type struct book_bank. Each of these variables can have the four
members of the structure book_bank.
INITIALIZING
STRUCTURES
A structure can be initialized in the same way how the other
variables are initialized. Initializing a structure means assigning some
values to the members of the structure. The initial values are given
enclosed in the braces and are separated by commas, shown as follows:

Eg:

struct employee

{ struct employee e = { 45,”Ajay Kumar”, 25000};

int empno;

char name[20];

float salary;

};
MEMORY
REPRESENTATION
E 145 Ajay Kumar 15000

empno name Salary


EXAMPLE PROGRAM FOR
STRUCTURES:
#include <stdio.h>
struct student
{
int age,regno;
char name[50];
};
int main()
{
struct student e={1,10,”ramu”};
printf("%d,%d,%s",e.age,e.regno,e.name);
return 0;
}
EXAMPLE PROGRAM FOR
STRUCTURES:
#include <stdio.h>
#include<string.h>
int main()
{
struct student
{
int age,regno;
char name[50];
}e;
e.age=10;
e.regno=20;
strcpy(e.name,"ramu");
printf("%d,%d,%s",e.age,e.regno,e.name);
return 0;
}
EXAMPLE PROGRAM FOR
STRUCTURES:
#include <stdio.h>
// Define a structure to represent a student
struct Student {
char name[50]; // To store the name of the student
int age; // To store the age of the student
float gpa; // To store the GPA of the student
};
int main() {
// Declare a structure variable of type Student
struct Student student1; printf("\nStudent Information:\n");
// Assign values to the fields printf("Name: %s\n", student1.name);
printf("Enter student's name: "); printf("Age: %d\n", student1.age);
scanf("%s", &student1.name); printf("GPA: %.2f\n", student1.gpa);
printf("Enter student's age: "); return 0;
scanf("%d", &student1.age); }
printf("Enter student's GPA: ");
scanf("%f", &student1.gpa);
OPERATIONS ON
STRUCTURES
Once a structure is defined, you can perform several
operations on it. These operations include:

 Accessing members of the structure

 Assigning values to members

 Copying structures

 Passing structures to functions

 Returning structures from functions

 Array of structures

 Pointer to structures
ACCESSING STRUCTURE ELEMENTS
 Members/elements of the structure are accessed using an operator
“.” which is known as member access operator or direct
selection operator.
struct book_bank
{
char title[25];
char author[20];
int pages;
float price;
} book1;

book1.price is the variable representing the price of book1 and can


be treated like any other ordinary variables.
EXAMPLE PROGRAM FOR ACCESSING STRUCTURES
ELEMENTS:
#include <stdio.h>
// Define a structure to represent a student
struct Student {
char name[50];
int age;
float gpa;
};
int main()
{
struct Student student1;
// Assign values to structure members
student1.age = 20;
student1.gpa = 3.5;
printf("Student Age: %d\n", student1.age);
printf("Student GPA: %.2f\n", student1.gpa);
return 0;
}
ASSIGNING VALUES TO MEMBERS
You can assign values to the structure members
in two ways.

 Either individually using the dot operator.

 Using the aggregate notation (structure


initialization).
EXAMPLE PROGRAM FOR ASSIGNING VALUES TO
MEMBERS:
#include <stdio.h>
struct Student { // Define a structure to represent a student
char name[50];
int age;
float gpa; };
int main() {
// Assigning values using the dot operator
struct Student student1;
student1.age = 20;
student1.gpa = 3.75;
// Assigning values using initialization (aggregate notation)
struct Student student2 = {"Alice", 22, 3.85};
printf("Student 1 Age: %d, GPA: %.2f\n", student1.age, student1.gpa);
printf("Student 2 Name: %s, Age: %d, GPA: %.2f\n", student2.name, student2.age,
student2.gpa);
return 0;
}
EXAMPLE PROGRAM FOR COPYING STRUCTURES:
#include <stdio.h>
struct Student { Note: You can directly assign one
structure
char name[50]; variable to another structure variable of the
int age; same type.
float gpa;
};
int main() {
struct Student student1 = {"John", 20, 3.75};
struct Student student2;
// Copy structure
student2 = student1;
printf("Student 2 Name: %s, Age: %d, GPA: %.2f\n", student2.name,
student2.age, student2.gpa);
return 0;
PASSING STRUCTURES TO
FUNCTIONS
You can pass structures to functions in two ways.

 Either by value (a copy of the structure is passed)

 By reference (using pointers).


EXAMPLE PROGRAM PASSING STRUCTURES TO
FUNCTIONS BY VALUE:
#include <stdio.h>
struct Student {
char name[50];
int age;
float gpa; };
// Function to display student details
void displayStudent(struct Student s) {
printf("Name: %s, Age: %d, GPA: %.2f\n", s.name, s.age, s.gpa);
}
int main() {
struct Student student1 = {"John", 20, 3.75};
// Pass structure to function by value
displayStudent(student1);
return 0;
}
EXAMPLE PROGRAM PASSING STRUCTURES TO FUNCTIONS
BY REFERENCE:
#include <stdio.h>
struct Student {
char name[50];
int age;
float gpa; };
// Function to modify student details using pointer to structure
void modifyStudent(struct Student *s) {
s->age = 21;
s->gpa = 3.9; }
int main() {
struct Student student1 = {"John", 20, 3.75};
// Pass structure to function by reference
modifyStudent(&student1);
printf("Modified Student Age: %d, GPA: %.2f\n", student1.age,
student1.gpa);
EXAMPLE PROGRAM FOR ARRAY OF STRUCTURE:
#include <stdio.h>
struct Student { Note: You can create an
array of
char name[50]; structures to store multiple records
int age; of the same type.
float gpa; };
int main() {
// Declare an array of structures
struct Student students[3] = { {"John", 20, 3.75},{"Alice", 22, 3.85},
{"Bob", 21, 3.90} };
// Print the details of each student
for (int i = 0; i < 3; i++) {
printf("Student %d: %s, Age: %d, GPA: %.2f\n", i + 1,
students[i].name, students[i].age, students[i].gpa);
}
return 0;
DIFFERENCE BETWEEN ARRAY AND
STRUCTURE
EXAMPLE PROGRAM FOR POINTER TO STRUCTURE:
#include <stdio.h>
struct Student {
char name[50];
int age;
float gpa;
};
int main()
{
struct Student student1 = {"John", 20, 3.75};
struct Student *ptr = &student1;
// Access structure members using pointer
printf("Student Age (using pointer): %d\n", ptr->age);
printf("Student GPA (using pointer): %.2f\n", ptr->gpa);
return 0;
}
DIFFERENCE BETWEEN POINTER AND
STRUCTURE
UNIONS
Union is a user defined data type that allows memory to be
shared by multiple data items of different data types.
Memory allocated to the union is equal to the memory of the
data member that has the largest size, and it is shared by all the other
members of the union.
DECLARATION OF
UNION
union union_name

data-type

member1;

data-type union item

member2; {

int m;

……………………….. float x;

data-type char c;

memberN; };

};
DECLARING UNION
VARIABLES
Below is the syntax for declaring the union
union <union_name ><variable-name>;
Ex:
Union:-
union item
{
int m;
float x;
char c;
};
Union variable:- union item code;
MEMORY
REPRESENTATION
union item
{
int m;
float x;
char c;
};
65497 65498 65499 65500

c
EXAMPLE PROGRAM FOR
UNIONS:
#include <stdio.h>
// Define a union
union Data {
int i;
float f;
char str[20];
};
int main() {
// Create a union variable
union Data data;
// Assign an integer value
data.i = 10;
printf("data.i : %d\n", data.i);
// Assign a float value (this overwrites the integer value)
data.f = 220.5;
printf("data.f : %.1f\n", data.f);
// Assign a string (this overwrites the float value)
strcpy(data.str, "C Programming");
printf("data.str : %s\n", data.str);
return 0;
}
STRUCTURES
VERSUS

UNIONS
Difference Structure Union

Keyword The keyword struct is used The keyword union


to define a structure is used to define a
union
Memory Each member within the Memory allocated to
structure is assigned a the union is shared
separate memory location all the members of
the union

Size The size of the structure is The size of the union


equal to the sum of the is equal to the size
sizes of its members of the largest
member of the
union
Difference Structure Union
Value Altering the value of one Alter the value of
Altering member of structure does any of the member
not affect the members of will affect the other
the structure member values

Accessing Individual members can be Only one member


members accessed at a time can be accessed at
a time
Initialization All the members of a Only one member
of Members structure can be initialized of a union is
at a time initialized.

You might also like