Vous êtes sur la page 1sur 32

Structure

Group of data items of different


data types held together in a
single unit.

Declaring a Structure
struct sname
{
type var1;
type var2;
type var3;
.
.
type varN;
};

struct is a keyword to define a structure.


sname is the name given to the structure data
type.
type is a built-in data type.
var1,var2,var3,..,varN are elements of
structure being defined.

Structure
template
sname is called tag makes it possible to declare
other variable of the same structure type without
having to rewrite the template itself. Its a type
name.tag is optional.

Eg to store information of an employee, our structure


declaration may look like as shown overleaf

struct employee_type
{
int code;
char name[20];
int dept_code;
float salary;
};
No variable has been associated with this
structure
No memory is set aside for this structure.

Declaring Variables of the Structure


Type
Declares a variable employee of type
employee_type
struct employee_type employee;
At this point, the memory is set aside for the structure
variable employee.

We can also declare variable(s) of structure type


along with the structure declaration.
struct employee_type

{
int code;
char name[20];
int dept_code;
float salary;
}employee

Consider the declarations to understand how the


elements of the structure variables are stored in
memory

struct example_type
{
char var1;
int var2;
float var3;
double var4;
};
struct example_type sample1;
Note: all members are stored in contiguous memory location in order in which
they are declared.

How the elements of the structure variables


are stored in memory
1197
1198
var
1

1199
1200
1201

var
2

1202
1203
1204
1205

var
3

1206
1207
1208

var
4

1209
1210
1211

Intializing Structures
struct student_type
{
int rollno;
char name[25];
int age;
float height;
};
struct student_type student={1000,Surbhi
salaria,18,5.6};

Accessing Structure Elements


Elements are accessed using dot operator.
It provides a powerful and clear way to refer to
an individual element.
Syntax: sname.vname
sname is structure variable name.
vname is name of the element of the
structure.
Eg: the element of the structure variable
student can be accessed as

student.rollno,student.name,student.age,student.
height

Global declaration of Structures


struct employee_type
{
int code;
char name[25];
char dept[15];
float salary;
};
main()
{
struct employee_type employee;
printf(\nEnter employee code:\n);
scanf(%d,&employee.code);
printf(\nEnter name:\n);
gets(employee.name);
printf(\nEnter employees dept:\n);
gets(employee.dept);
printf(\nEnter employees salary:\n);
scanf(%f,&employee.salary);
continue

continue
printf(\n\nParticulars of emp as entered by user\n);
printf(\nEmployees code:%d,employee.code);
printf(\nEmployees name:%s,
employee.name);
printf(\nEmployees dept:%s,employee.dept);
Printf(\nEmployees sal:%f,employee.salary);
}

Local declration of structure


main()
{
struct employee_type
{
int code;
char name[25];
char dept[15];
float salary;
};
struct employee_type employee;
printf(\nEnter employee code:\n);
scanf(%d,&employee.code);
printf(\nEnter name:\n);
gets(employee.name);
printf(\nEnter employees dept:\n);
gets(employee.dept);
printf(\nEnter employees salary:\n);
scanf(%f,&employee.salary);
printf(\n\nParticulars of emp as entered by user\n);

printf(\nEmployees code:%d,employee.code);
printf(\nEmployees name:%s, employee.name);
printf(\nEmployees dept:%s,employee.dept);
Printf(\nEmployees sal:%f,employee.salary);
}

Use of Assignment Statement for


Structures
Value of one structure variable can be
assigned to another variable of the same type
using simple assignment statement.if student1
and student2 are structure variable of type
student_type,then
student2=student1;
Assigns value of structure variable student1 to
student2

Simple assignment cannot be used this way


for arrays.

Nested structure
#include <stdio.h>
#include <conio.h>
struct stud_Res
{
int rno;
char std[10];
struct stud_Marks
{
char subj_nm[30];
int subj_mark;
}marks;
}result;
void main()
{
clrscr();
printf("\n\t Enter Roll Number : ");
scanf("%d",&result.rno);
printf("\n\t Enter Standard : ");
scanf("%s",result.std);
printf("\n\t Enter Subject Code : ");
scanf("%s",result.marks.subj_nm);
printf("\n\t Enter Marks : ");
scanf("%d",&result.marks.subj_mark);
printf("\n\n\t Roll Number : %d",result.rno);
printf("\n\n\t Standard : %s",result.std);
printf("\nSubject Code : %s",result.marks.subj_nm);
printf("\n\n\t Marks : %d",result.marks.subj_mark);
getch();
}

Structure and Function


The relationship of structure with the
function can be viewed from three angles:-

1.Passing Structures to a function.


2.
Function Returning Structure.
3.Passing array of Structures to Function.

Passing Structure to a Function


Similar to passing array of variable,structure
can be passed to a function as argument
Syntax:
type-specifier func-name(struct-variable);
argument*/

/*actual

Read and display student grade


by using structure with function
Void display(struct student m)
struct student
{
{
printf(\nRollno is %d,m.rn);
int rn;
printf(\n Name is %s,m.name);
char name[20];
printf(\n Grade is: %c,m.grade);
char grade;
}s;
}
Void display(struct student m);
main()
{
printf(\nEnter rollno,name and grade of student:\n);
scanf(%d %s %c,&s.rn,s.name,&s.grade);
display(s);
getch();
}

Passing of structure variables by value to a


struct date
function
{
int day;
int month;
int year;
};
void print_date(struct date a);
void main()
{
struct date d={10,12,1997};
print_date(d);
}
void print_date(struct date a)
{
printf(\nDate in format %d %d %d,a.day,a.month,a.year);
}

Pointers and Structures


struct student_type student,*ptr
It declares a structures variable student and a pointer variable
ptr to structure of type student_type.
ptr can be initialized with the following assignment statement
ptr=&student;
HOW WE CAN ACCESS THE ELEMENTS OF
STRUCTURE?

*ptr.rollno,*ptr.name,*ptr.age,*ptr.height

But this approach will not work because dot has higher
priority
Correctly way to write is:

(*ptr).rollno,(*ptr).name,(*ptr).age,(*ptr).height
or

ptr->rollno,ptr->name,ptr->age,ptr->height

Passing of structure variables by reference to a


function
struct date
{
int day;
int month;
int year;
};
void get_date(struct date *a);
main()
{
struct date d;
printf(Enter date in the format:\n);
get_date(&d);
printf(\nDate entered by you %d %d %d,d.day,d.month,d.year);
}
void get_date(struct date *a)
{ scanf(%d %d %d,&a->day,&a->month,a->year);
}

Function Returning Structure


struct date
{
struct date get_date(void)
int day;
{
int month;
struct date a;
int year;
scanf(%d %d %d,&a.day,&a.month,&a.year);
};
return a;
struct date get_date(void);
}
main()
{
struct date d;
printf(\nEnter date in format day/month/year);
d=get_date();
printf(\nDAte entered by you is %d %d %d\n,d.day,d.month,d.year);
}

Problem ???
Suppose we want to store details about all
the employees of CIVIL department.
We need to store----name, designation,
biometric-id, salary.
st
So we will create 1 array for storing the
name of all the employees, 2nd array for
storing the designation , 3rd array for storing
biometric id and 4th array for storing the
salary of all the employees.

Array of Structures

If we wish to process a list of values of


structure type , then we need an array of
such structures.

Declaring an Array of Structures


struct employee_type
{
int code;
char name[25];
float salary;
};
struct employee_type employee[50];
Contd

Accessing elements an array of structures


Void main()
{
Int i;
For(i= 0; i<50; i++)
{
Scanf(%d, & employee[i]. Code);
Scanf(%s, & employee[i].name);
Scanf(%f, & employee[i].salary);
}
printf(\n details of all employees are= );
For(i= 0; i<50; i++)
{
printf(%d, employee[i]. Code);
printf(%s, employee[i].name);
printf(%f, employee[i].salary);
}
Linkfloat ()
{
Float a=0; *b;
B=&a;
A=*b;
}
Printf(\n main function ends);
}

Self referential Structures


Do from your notebook..

Union
Union:
Union is similar as structure. The major distinction between
them in terms of storage.
In structure each member has its own storage location whereas
all the members of union uses the same location.
The union may contain many members of different data type it
can handle only one member at a time union can be declared using
the keyword union.

Union item
{
int m;
float x;
char c;
} code;
This declare a variable code of type union item.

UNION Example
union employee_type
{
int code;
char name[25];
char dept[15];
float salary;
};
main()
{
union employee_type employee;
printf(\nEnter employee code:\n);
scanf(%d,&employee.code);
printf(\nEnter name:\n);
gets(employee.name);
printf(\nEnter employees dept:\n);
gets(employee.dept);
printf(\nEnter employees salary:\n);
scanf(%f,&employee.salary);
printf(\n\nParticulars of emp as entered by user\n);
printf(\nEmployees code:%d,employee.code);
printf(\nEmployees name:%s, employee.name);
printf(\nEmployees dept:%s,employee.dept);
Printf(\nEmployees sal:%f,employee.salary);
}

Limitation of union
We can not intialize the elements of union.

Benefit of union
Less memory consumption as compared to
structures.

Bit field
Consume less memory.
Can be used only with integers.
Int b;
In this case , 2 bytes(16 bits) are assigned
to b variable.
Unsigned int b:3;
In this case , 3 bits will be assigned to b
variable.

Benefit of bit field


Saves memory

Disadvantage of bit field


Can not used with scanf
Can not used with array
Can not used with pointers.
We can not obtain the address of bit field
variable,

Vous aimerez peut-être aussi