Module 5 - Pointer,Structures and Union.pptm.pdf

SudipDebnath20 8 views 29 slides Jun 13, 2024
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

pointers in c


Slide Content

PRPEARED BY: PROF. NEHA HARDE ©
C PROGRAMMING
Module 5

Structure,Union and Pointer
Course Outcome (CO5):
Demonstrate the use of structure and union in C language

PRPEARED BY: PROF. NEHA HARDE ©
❖Introduction of structure
❖Structure Declaration
❖Structure Variable
❖Structure Variable Initialization
❖Structure elements in memory
❖Structure Example
❖Typedef
❖Array of structures
❖Nested Structure
❖Passing Structure to function
❖Assigning value of one structure to another







Structure

PRPEARED BY: PROF. NEHA HARDE ©
INTRODUCTION OF STRUCTURE
Array is used to store several elements of same type
eg. Roll no. of students can be stored in an array of type int
int roll_no[60];

Problem Statement:
Store students information like roll no., name, department, college, marks
Roll No. – int
Name – char[10]
Department – char[5]
Marks – float

There are two approaches:
1.Construct individual arrays, one for roll no, one for name, one for class…..
2.Use a structure

PRPEARED BY: PROF. NEHA HARDE ©
STRUCTURE DECLARATION
Defining a structure:
Each element in a structure is called as member
struct structure_name
{
structure element 1;
structure element 2;
structure element 3;
.
.
.
};
Syntax
struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
};
Example
Structure is a user defined data type that can hold different types of data together in a single type

PRPEARED BY: PROF. NEHA HARDE ©
STRUCTURE VARIABLE
❖Structure is user defined data type, variables of that type can be declared
Different ways to declare variables are -


struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
}s1, s2, *s3;
struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
struct student s1;
struct student s2;

struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
struct student s1, s2;

PRPEARED BY: PROF. NEHA HARDE ©
STRUCTURE VARIABLE INITIALIZATION
struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
struct student s1={10, ”Neha”, “IT” 90.50};
struct student s2={12, “Ram”, “Comp”, 85.00};
struct student s3 = {0};
❖Structure elements can be accessed using dot
operator

Syntax:
structureVariable.structureElement
Example:
s1.roll_no
s1.dept

PRPEARED BY: PROF. NEHA HARDE ©
STRUCTURE ELEMENTS IN
MEMORY
struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
struct student s1={10, ”Neha”, “IT”, 90.50};

100 104 114 119
s1.roll_no s1.student_name s1.dept s1.marks
123

PRPEARED BY: PROF. NEHA HARDE ©
STRUCTURE EXAMPLE
Problem statement: Define structure student with members roll_no, student_name,
dept, marks and accept student record from user and display student record.









struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
struct student s1={10, ”Neha”, “IT”, 90.50};

int main()
{
struct student s2;
printf(“\n Enter s2 information:”);
scanf(“%d %s %s %f”, &s2.roll_no, s2.student_name, s2.dept, &s2.marks);
printf(“\n Displaying s1 record: %d %s %s %.2f”, s1.roll_no, s1.student_name, s1.dept,s1.marks);
printf(“\n Displaying s2 record: %d %s %s %.2f”, s2.roll_no, s2.student_name, s2.dept,s2.marks);
return 0;
}

PRPEARED BY: PROF. NEHA HARDE ©
TYPEDEF
❖typedef is a keyword used to assign alternative names to existing datatypes
❖It is mostly used with user defined data types, when names of the data types become complicated to use in programs
Syntax: typedef existing_datatype new_datatype









struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
};
struct student s1={10, ”Neha”, “IT”, 90.50};
struct student s2={12, “Ram”, “Comp”, 85.00};
typedef struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
}stud;
stud s1={10, ”Neha”, “IT”, 90.50};
stud s2={12, “Ram”, “Comp”, 85.00};
Old Data type
New Data type

PRPEARED BY: PROF. NEHA HARDE ©
TYPEDEF
Problem statement: Define structure student with members roll_no, student_name,
dept, marks and accept student record from user and display student record.









typedef struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
}stud;
stud s1={10, ”Neha”, “IT”, 90.50};

int main()
{
stud s2;
printf(“\n Enter s2 information:”);
scanf(“%d %s %s %f”, &s2.roll_no, s2.student_name, s2.dept, &s2.marks);
printf(“\n Displaying s1 record: %d %s %s %.2f”, s1.roll_no, s1.student_name, s1.dept,s1.marks);
printf(“\n Displaying s2 record: %d %s %s %.2f”, s2.roll_no, s2.student_name, s2.dept,s2.marks);
return 0;
}

PRPEARED BY: PROF. NEHA HARDE ©
ARRAY OF STRUCTURES
Problem Statement: Store information of 60 students and print it.
Solution: Use Array of Structure






struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
};
struct student s1={10, ”Neha”, “IT”, 90.50};
struct student s2={12, “Ram”, “Comp”, 85.00};
…….
typedef struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
}stud s[60];

PRPEARED BY: PROF. NEHA HARDE ©
ARRAY OF STRUCTURES

S[0].roll_no S[0].student_name S[0].dept S[0].marks

S[1].roll_no S[1].student_name S[1].dept S[1].marks

S[2].roll_no S[2].student_name S[2].dept S[2].marks

PRPEARED BY: PROF. NEHA HARDE ©
ARRAY OF STRUCTURES
typedef struct student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
}stud;
stud s[5];

int main()
{
int i;
printf(“\n Enter Student information:”);
for(i=0; i<5; i++)
{
printf(“\n Enter values of s[%d] record: ”, i+1);
scanf(“%d %s %s %f”, &s[i].roll_no, s[i].student_name, s[i].dept, &s[i].marks);
printf(“\n Displaying s[%d] record: %d %s %s %.2f”, i+1, s[i].roll_no, s[i].student_name, s[i].dept, s[i].marks);
}
return 0;
}

PRPEARED BY: PROF. NEHA HARDE ©
NESTED STRUCTURES









Print student s1 record:
printf(“Roll no = %d Name= %s Dept = %s Marks = %.2f City = %s Pin = %d ”, s1.roll_no, s1.student_name, s1.dept, s1.marks,
s1.add.city, s1.add.pin);







struct address
{
char city[10];
int pin;
};

struct address add;

struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
struct address add;
};
struct student s1 = {10, “Neha”, “IT”, 90.50, “Mumbai”, 400011};

PRPEARED BY: PROF. NEHA HARDE ©
NESTED STRUCTURES
struct address
{
char city[10];
int pin;
};
struct address add;

struct student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
struct address add;
};
struct student s1 = {10, “Neha”, “IT”, 90.50, “Mumbai”, 400011}

int main()
{
printf(“\n Displaying s1 record: ”);
printf(“\n %d %s %s %f %s %d”, s1.roll_no, s1.student_name, s1.dept, s1.marks, s1.add.city, s1.add.pin);
return 0;
}

PRPEARED BY: PROF. NEHA HARDE ©
PASSING STRUCTURE TO FUNCTION
Two ways to pass structure to a function
❖Passing individual structure members to a function

❖Passing an entire structure to a function

PRPEARED BY: PROF. NEHA HARDE ©
PASSING INDIVIDUAL MEMBER TO A FUNCTION
#include<stdio.h>
typedef struct student
{
int rollno;
char name[20];
float marks;
}stud;
int main()
{
stud s1 = {11, “Mohit”, 98.7};
printf("\nDisplay student information\n");
display(s1.rollno, s1.name, s1.marks); return 0;
}
void display(int r, char n[20], float m)
{
printf("%d %s %f ",r, n, m);
}

PRPEARED BY: PROF. NEHA HARDE ©
PASSING ENTIRE STRUCTURE TO A FUNCTION
#include<stdio.h>
typedef struct student
{
int rollno;
char name[20];
float marks;
}stud;
int main()
{
stud s1 = {11, “Mohit”, 98.7};
printf("\nDisplay student information\n");
display(s1);
return 0;
}
void display(stud s)
{
printf("%d %s %f ",s.rollno, s.name, s.marks);
}

PRPEARED BY: PROF. NEHA HARDE ©
PASSING ENTIRE STRUCTURE TO A FUNCTION
typedef struct student
{
int rollno;
char name[20];
float marks;
}stud s1;

stud *ptr = &s1;
Structure and Pointer
❖Structure members can be accessed using arrow
operator (->) when they are accessed by pointer

PRPEARED BY: PROF. NEHA HARDE ©
PASSING ENTIRE STRUCTURE TO A FUNCTION
#include<stdio.h>
typedef struct student
{
int rollno;
char name[20];
float marks;
}stud;
int main()
{
stud s1 = {11, “Mohit”, 98.7};
printf("\nDisplay student information\n");
display(&s1);
return 0;
}
void display(stud *ptr)
{
printf("%d %s %f ",ptr->rollno, ptr->name, ptr->marks);
}
Structure and Pointer

PRPEARED BY: PROF. NEHA HARDE ©
ASSIGNING VALUE OF ONE STRUCTURE TO ANOTHER
typedef struct student
{
int rollno;
char name[20];
float marks;
}stud s1,s2,s3;
stud s1 = {1, “naina”, 80.5};
❖Assigning all elements at a time:
s2 = s1;

❖Assigning one elements value at a time:
s3.rollno = s1.rollno;
strcpy(s3.name,s1.name);
S3.marks = s1.marks;

PRPEARED BY: PROF. NEHA HARDE ©
❖Introduction of Union
❖Union variable
❖Accessing union members
❖Memory allocation
❖Union example
❖Structure vs Union


Union

PRPEARED BY: PROF. NEHA HARDE ©
INTRODUCTION OF UNION
Union is a user-defined data type which can hold collection
of different type of data in the same memory location
Defining a union:
union keyword is used for defining a union having members of different type
union union_name
{
union element 1;
union element 2;
union element 3;
.
.
.
};
Syntax
union student
{
int roll_no;
char student_name[10];
char dept[10];
float marks;
};
Example

PRPEARED BY: PROF. NEHA HARDE ©
UNION VARIABLE
❖Union is user defined data type, variables of that type can be declared
Different ways to declare variables are -


union student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
}s1, s2, s3;
union student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
union student s1;
union student s2;

union student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
union student s1, s2;

PRPEARED BY: PROF. NEHA HARDE ©
ACCESSING UNION MEMBERS
union student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};
union student s1, *s2;
❖Union elements can be accessed using dot operator (Using Variable s1)
Syntax: unionVariable.unionElement
Example:
s1.roll_no
s1.dept
❖Union elements can be accessed using arrow operator
(Using Pointer variable *s2)
Syntax: unionVariable->unionElement
Example:
s2->roll_no
s2->dept

PRPEARED BY: PROF. NEHA HARDE ©
MEMORY ALLOCATION
❖Memory is not allocated while defining a union but it is allocated while declaring a variable of union
❖All union elements share same memory
❖Memory equal to the size of largest element is allotted to union
❖Union can contain different types of elements but not all elements can be initialized at a time
❖Only one element can be accessed at a time
union student
{
int roll_no;
char student_name[10];
char dept[5];
float marks;
};

101
student_name = 10 bytes
110

PRPEARED BY: PROF. NEHA HARDE ©
UNION EXAMPE
union student
{
int roll_no;
char student_name[10];
};

int main()
{
union student s2;
printf(“\n Enter rollno:”);
scanf(“%d”, &s2.roll_no);
printf(“%d”, s2.roll_no);
printf(“\n Enter name”);
scanf(“%s”, s2.student_name);
printf(“%s”, s2.student_name);
return 0;
}

Invalid Initialization:

union student s1={10, “Neha”};

PRPEARED BY: PROF. NEHA HARDE ©
STRCTURE VS UNION
union student
{
char name[40];
int roll_no;
int phone_number
};
struct student
{
char name[40];
int roll_no;
int phone_number
};

PRPEARED BY: PROF. NEHA HARDE ©
STRCTURE VS UNION