Académique Documents
Professionnel Documents
Culture Documents
Declaring a Structure
struct sname
{
type var1;
type var2;
type var3;
.
.
type varN;
};
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.
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.
{
int code;
char name[20];
int dept_code;
float salary;
}employee
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.
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};
student.rollno,student.name,student.age,student.
height
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);
}
printf(\nEmployees code:%d,employee.code);
printf(\nEmployees name:%s, employee.name);
printf(\nEmployees dept:%s,employee.dept);
Printf(\nEmployees sal:%f,employee.salary);
}
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();
}
/*actual
*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
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
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.