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

C 07 (Structure)

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

C 07 (Structure)

structures
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

STRUCTUR

E
C supports structure which allows you to
wrap one or more variables with
different data types.

Structure is composition of the different


variables of different data types , grouped
under same name .

A structure can contain any valid data types


like int, char, float even arrays or even
other structures.

Structure is for storing complicated data.


DECLARATION
Syntax:
struct structure_name
{
member1;
member2;
member3;
};
Struct keyword is used to declare structure.

Name given to structure is called as tag.

Variables declared inside the structure is


called structure member.

Declaration of structure reserves no space.

Structure member may be of different data


type including user defined data-type.

Structure members are accessed using


structure variable.
Example:

struct date
{
int date;
char month[20];
int year;
}today;
struct date
{
int date;
char month[20];
int year;
};
struct date today;
Multiple Structure Variables

struct Book
{
int pages;
char name[20];
int year;
}c,cpp,java;
IMPORTANT RULES
Closing brace of structure type
declaration must be followed by semi
colon.
Don’t forget to use struct keyword.
Structures are written in global
declaration section.
it is not necessary to initialize all
members of structure.
INITIALIZATION

struct student
{
char name[20];
int roll;
float marks;
}std1 = {“xxx",67,78.3);
struct student
{
char name[20];
int roll;
float marks;
}
std1 = {“xxxx",89,98.5);
std2 = {“xxx",92,87.3);
struct student
{
char name[20];
int roll;
float marks;};

void main()
{
struct student s1 =
{“xxxx”,100,95};
- - - - --
- - - - --
- - - - --
}
Accessing structure
member
Array elements are accessed using
subscript variable and structure
members are accessed using dot
operator [.].

[.] is called structure member operator.

This operator is used between


structure name and member name.
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float mark;
}s={"xxx",23,89.4};
void main()
{
clrscr();
printf(" Name:%s",s.name);
printf("\n Roll No:%d",s.rollno);
printf("\n Mark:%f",s.mark);
getch();

}
#include<stdio.h>
struct student
{
char name[20];
int rollno;
float mark;
};
void main()
{
struct student s;
clrscr();
s.name[]="yyyy";
s.rollno=12;
s.mark=95;
printf("%s",s.name);
printf("%d",s.rollno);
printf("%f",s.mark);
getch();
}
Structure members are accessed by using
arrow operator when a structure variable
is pointer type.
#include<stdio.h>
struct student
{
int rollno;
float mark;
}*s;
void main()
{
clrscr();
s->rollno=11;
s->mark=50.4;
printf("\n Roll No:%d",s->rollno);
printf("\n Mark:%f",s->mark);
getch();
}
NESTING OF
STRUCTURE
Structure written inside another
structure is called as nesting of
two structures.

A structure can be the member


of another structure
struct date
{
int date;
int month;
int year;
};

struct employee
{
char ename[20];
float salary;
struct date doj;
}emp;
struct date void main()
{ {
clrscr();
int date;
emp.ename=“deepa”;
int month; emp.salary=4565.90;
int year; emp.doj.date=12;
};
printf(“%s”,emp.name
);
struct employee
{ printf(“%f”,emp.salary
);
char ename[20];
float salary; printf(“%d”,emp.doj.d
struct date doj; ate);
}emp; }
Array of Structure
Structure is used to store the
information of One particular object
To store more than one objects Array
of Structure is used.

struct <structure Name>


{
data type member1;
data type member2;
}structure variable[array size];
#include<stdio.h> for(i=0;i<n;i++)
#include<conio.h> {
printf(“Enter Roll Number”);
struct student
scanf(“%d”,&stud[i].roll_no);
{ printf(“Enter student
int roll_no; name”);
int age; scanf(“%s”,&stud[i].name);
printf(“Enter age”);
char name[20];
scanf(“%d”,&stud[i].age);
int m1,m2,m3; printf(“Enter three marks”);
int tot; scanf(“%d%d
float avg; %d”,&stud[i].m1,&stud[i].m
2,&stud[i].m3);
}stud[50];
void main() stud[i].tot=stud[i].m1+stud[
i].m2+stud[i].m3;
{ stud[i].avg=stud[i].tot/3;
int i,n; printf(“\n Total Mark:
clrscr(); %d”,stud[i].tot);
printf(“\n Average of mark:
printf(“Enter no of %f”,stud[i].avg);
students to be getch();
calculated”); }
scanf(“%d”,&n);
Passing
structure to
function
Structure can be passed
to function as a
parameter.
#include<stdio.h> void main()
#include<conio.h> {
struct sample int i;
{ clrscr();
int num1; for(i=0;i<3;i++)
int num2; {
}s[3]; get(&s[i]);
void get(struct sample *sptr) }
{ for(i=0;i<3;i++)
printf("\nEnter num1 : ");
{
scanf("%d",&sptr->num1);
print(&s[i]);
printf("\nEnter num2 : ");
}
scanf("%d",&sptr->num2);
getch();
}
}
void print(struct sample
*sptr)
{
printf("\nNum1 : %d",sptr-
>num1);
printf("\nNum2 : %d",sptr-
>num2);
}
#include<stdio.h>
#include<conio.h>
struct student
{
int roll_no;
int age;
char name[20];
int m1,m2,m3;
int tot;
float avg;
char re[10],cl[10];
};
main()
{
struct student result(struct student,int);
student stud,stud1;
int i,n;
clrscr();
printf(“Enter no of students to be calculated”);
scanf(“%d”,&n);
printf(“Enter Roll Number”);
scanf(“%s”,&stud.roll_no);
printf(“Enter student name”);
scanf(“%s”,&stud.name);
printf(“Enter age”);
scanf(“%s”,&stud.age);
printf(“Enter three marks”);
scanf(“%d%d%d”,&stud.m1,&stud.m2,&stud.m3);
stud1=result(stud,n);
printf(“\n Total Mark:%d”,stud1.tot);
printf(“\n Average of mark:%f”,stud1.avg);
printf(“\n Result: %s, ”,stud1.re);
printf(“\n Class: %s,stud1.cl);
getch();
}
struct student result(struct student st,int n)
{
st.tot=st.m1+st.m2+st.m3;
st.avg=st.tot/3.0;
if(st.m1<35 && st.m2<35 && st.m3<35)
{
strcpy(st.re,”Fail”);
strcpy(st.cl,”Nil”)
}
else
{
strcpy(st.re,”PASS”);
if(st.avg>=80)
strcpy(st.cl,”First”);
else if (st.avg>=60)
strcpy(st.cl,”Second”);
else
strcpy(st.cl,”Third”);
}
return(st);
}

You might also like