C 07 (Structure)
C 07 (Structure)
E
C supports structure which allows you to
wrap one or more variables with
different data types.
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 [.].
}
#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.
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.