Module 5
Module 5
struct bookbank
{
char author[50];
char title[50];
int year;
float price;
};
In the above example bookbank is structure name.
The title, author, price, year are structure members.
Declaring a structure variable-
• After defining a structure format we can declare variable for that type.
• Example :-
struct employee emp1;
Here struct employee is user defined data type and emp1 is structure
variable.
Another Example-
Two variables of the same structure type can be copied the same
way as ordinary variables.
If emp1 and emp2 belong to the same type, then the following
statement is valid. emp1 = emp2, and emp2 = emp1;
However, the statements that are shown here:
e1 < e2; and e1 != e2; are not permitted.
Finding size of a structure
struct employee
{
int emp_no;
char empname[20];
int age;
float experience;
float emp_sal;
};
Method 1-
Size of structure= size of emp_no+size of empname+ size of age+ size
of experience+ size of emp_sal
= 2+ (20*1) + 2+ 4 +4=32 bytes.
Finding size of a structure
Method 2-Using sizeof() operator.
struct employee
{
int emp_no;
char empname[20];
int age;
float experience;
float emp_sal;
};
void main()
{
struct employee emp;
printf(“Size of structure= %d”, sizeof(emp));
}
• Nested Structures :- A nested structure is a structure that contains
another structure as its member.
Syntax :-
struct structure_name1
{
datatype member 1;
datatype member 2;
};
struct structure_name2
{
datatype member 1;
datatype member 2;
struct structure_name1 member3;
};
C program to read & display student information using nested structures.
#include<stdio.h>
struct DOB
{
int day;
int month;
int year;
};
struct student
{
int rollno;
char name[20];
struct DOB date;
};
void main()
{
struct student s;
printf("Enter Student Rollno :");
scanf("%d",&s.rollno);
printf("Enter Student Name :");
scanf("%s",s.sname);
printf("Enter date of birth as day month year:");
scanf("%d%d%d",&s.date.day,&s.date.month,&s.date.year);
• While invoking the function from main( )in the place of actual arguments we
can pass the structure member as an argument.
#include<stdio.h>
void add(int x,int y);
struct addition
{
int a;
int b;
};
void main()
{
struct addition sum;
printf(" Enter two numbers\n");
scanf("%d%d",&sum.a,&sum.b);
add(sum.a,sum.b);
}
• While invoking the function instead of passing the individual members the
entire structure, i.e. the structure variable is passed as a parameter to the
function.
#include<stdio.h>
struct addition
{
int a;
int b;
};
void add(struct addition sum);
void main()
{
struct addition sum;
printf(" Enter two numbers\n");
scanf("%d%d",&sum.a,&sum.b);
add(sum);
}
void add(struct addition sum)
{
int res;
res=sum.a+sum.b;
printf("Resultant=%d\n",res);
}
3. Passing Structures Through Pointers
Structure pointer is defined as the pointer which points to the address of the memory block
that stores a structure known as the structure pointer.
Ex-
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
int main()
{
struct Student s1;
struct Student* ptr = &s1;
s1.roll_no = 27;
strcpy(s1.name, "Kamlesh Joshi");
strcpy(s1.branch, "Computer Science And Engineering");
s1.batch = 2019;
printf("Roll Number: %d\n", (*ptr).roll_no);
printf("Name: %s\n", (*ptr).name);
printf("Branch: %s\n", (*ptr).branch);
printf("Batch: %d", (*ptr).batch);
return 0;
}
Unions
• A union is a special data type available in C that allows to store different
data types in the same memory location.
• Union can be defined with many members, but only one member can contain
a value at any given time.
• Syntax:
union union_name
{
datatype field_name;
datatype field_name; // more variables
}union_variable;
Accessing Union Members
#include <stdio.h>
union Job {
float salary;
int workerNo; Output
} j;
void main() Salary = 0.0
{ Number of workers = 100
j.salary = 12.3;
j.workerNo = 100; // when j.workerNo is assigned a value, j.salary will no
longer hold 12.3 size of both the data type is 4byte
Here, arr is an array of union which can hold two union elements.
Arrays of Union Variables
Array of union initialization:
union values arr[2] = {{1}, {2}};
Structures Inside Unions
Syntax-
FILE * fptr;
2. Opening an existing file-
• A file must first be opened before reading/writing data to it.
The file (both text and binary) should be closed after reading/writing.
Closing a file is performed using the fclose() function.
File Operations - open and close mode
File Operations – Write mode
Reading data from files-
Here, arr is an array of union which can hold two union elements.
Arrays of Union Variables
Array of union initialization:
union values arr[2] = {{1}, {2}};
Structures Inside Unions
Syntax-
FILE * fptr;
2. Opening an existing file-
• A file must first be opened before reading/writing data to it.
The file (both text and binary) should be closed after reading/writing.
Closing a file is performed using the fclose() function.
File Operations - open and close mode
File Operations – Write mode
Reading data from files-