You are on page 1of 11

01

02

C Language
LECTURE 40
04
Today’s Agenda

01 Structure (Part-2)

02
Some Example
Copying One Structure Variable To Another
struct Student
{ roll grade per
int roll;
char grade; s ? 10 ? ‘B’ ? 57.9
float per;
}; 8000 8004 8005 8009
int main()
{
struct Student s, p;
printf("Enter roll, grade and per: ");
scanf("%d %c %f", &s.roll, &s.grade, &s.per);
p.roll = s.roll; roll grade per
p.grade = s.grade; p ? 10 ? ‘B’ ? 57.9
p.per = s.per;
return 0; 9000 9004 9005 9009
}
Copying One Structure Variable To Another
#include <stdio.h>
struct Student roll grade per
{ In C language we can
? 10 ? ‘B’ ? 57.9
int roll; use assignment operator s
char grade; on structures also
float per; provided these structure 8000 8004 8005 8009
}; variables are of same
int main()
type
{
struct Student s, p;
printf("Enter roll, grade and per: ");
scanf("%d %c %f", &s.roll, &s.grade, &s.per); roll grade per
p = s; Single line shortcut p ? 10 ? ‘B’ ? 57.9
printf("%d %c %f", p.roll, p.grade, p.per);
return 0; 9000 9004 9005 9009
}
Creating Array of Structure
struct Student
{
int roll; roll grade per roll grade per roll grade per
char grade;
float per;
s 10 ‘A’ 71.9 20 ‘B’ 56.7 30 ‘C’ 41.9
};
int main()
2000 s[0] 2009 s[1] 2018 s[2] 2027
{
struct Student s[3];
int i; Output
for(i = 0; i < 3; i++) Enter roll, grade and per: 10 A 71.9
{
printf("Enter roll, grade and per: "); Enter roll, grade and per: 20 B 56.7
scanf("%d %c %f", &s[i].roll, &s[i].grade, &s[i].per); Enter roll, grade and per: 30 C 41.9
} 10 A 71.900002
for(i = 0; i < 3; i++)
printf("%d %c %f\n", s[i].roll, s[i].grade, s[i].per);
20 B 56.700001
return 0; 30 C 41.900002
}
Structures and Pointers
struct Student
{ roll grade per
int roll; s 10 ‘A’ 76.80
char grade; ->
float per; 9000 9004 9005 9009
};
int main()
{
struct Student s;
struct Student * p; Arrow 9000
1. Using structure variable
p = &s; Operator p
p -> roll = 10;
s.roll
p -> grade = 'A'; or
p -> per = 76.8f; 2. using structure pointer
printf("%d %c %f\n", p -> roll, p -> grade, p -> per); p->roll
return 0;
}
Accepting Input in Structure Using Pointer
struct Student
roll grade per
{
int roll; s 10 ‘A’ 76.80
char grade;
float per; 9000 9004 9005 9009
};
int main()
{
struct Student s; 9000
struct Student * p;
p = &s; p
printf("Enter roll, grade and per: ");
scanf("%d %c %f", &p->roll, &p->grade, &p->per);
printf("%d %c %f\n", p->roll, p->grade, p->per);
return 0;
}
Array of Structure and Pointer
struct Student
{
int roll; roll grade per roll grade per
char grade;
roll grade per
float per; s 10 ‘A’ 71.9 20 ‘B’ 56.7 30 ‘C’ 41.9
};
int main()
{ 2000 s[0] 2009 s[1] 2018 s[2] 2027
struct Student s[3];
struct Student * p;
p = s;
int i;
2000
for(i = 0; i < 3; i++) p
{
printf("Enter roll, grade and per: "); p +i p+I*7
scanf("%d %c %f", &(p + i)->roll, &(p + i)->grade, &(p + i)->per); 2000 + 0 * 9 2000
}
for(i = 0; i < 3; i++) 2000 + 1 * 9 2009
printf("%d %c %f\n", (p + i)->roll, (p + i)->grade, (p + i)->per); 2000 + 2 * 9 2018
return 0;
}
Structure and Functions
struct Student void display(struct Student p)
{ Pass By Value {
int roll; printf("%d %c %f\n", p.roll, p.grade, p.per);
char grade; }
float per; roll grade per
}; 10 ‘B’ 57.9
s
void display(struct Student);
int main() 8000 8004 8005 8009
{
struct Student s;
printf("Enter roll, grade and per: ");
scanf("%d %c %f", &s.roll, &s.grade, &s.per); roll grade per
display(s); p 10 ‘B’ 57.9
return 0;
} 9000 9004 9005 9009
Structure and Functions
struct Student void display(struct Student p)
{ Pass By Reference {
int roll; printf("%d %c %f\n", p.roll, p.grade, p.per);
char grade; }
float per;
};
void accept(struct Student *); roll grade per
void display(struct Student); 10 ‘A’ 76.80
int main()
s
{
struct Student s; 9000 9004 9005 9009
accept(&s);
display(s); accept() display()
return 0;
} roll grade per
void accept(struct Student * p) 9000 p 10 ‘A’ 76.80
{
printf("Enter roll, grade and per: "); p
scanf("%d %c %f", &p->roll, &p->grade, &p->per); Pass By Value
}
End of Lecture 40
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like