structure-unit-4 (1)
structure-unit-4 (1)
Q) Definition of structure:
A structure is a collection of different data types.
Q) Declaration and initialization of structure:
A structure is declared using the keyword “struct” keyword.
All variables of the structure are declared within the structure.
These variables are called members of the structure.
Syntax:
struct structure_name
{
datatype member1;
datatype memeber2
datatype member n;
};
Example:
struct student
{
int rollno;
char name[25];
float totalmark;
};
When a structure is defined, it creates a user-defined type but, no storage is allocated. For the
above structure of student, variable can be declared as:
Thus, the stud1 and stud2 are structure variables of type student. The above structure can hold
information of 2 students.
It is possible to combine the declaration of structure combination with that of the structure variables,
as shown below.
1
struct student
{
int rollno;
char name[25];
float totalmark;
}stu1,stu2;
Passing individual
members
The address location of structure variable is passed to function while passing it by reference. If
structure is passed by reference, change made in structure variable in function definition reflects in
original structure variable in the calling function.
Example:
#include<stdio.h>
#include<conio.h>
struct student
{
int no;
char name[10];
int fee;
};
void display(struct student *);
void main()
{
struct student s;
clrscr();
printf("enter student number,name and fee\n");
scanf("%d%s%d",&s.no,&s.name,&s.fee);
display(&s);
7
getch();
}
void display(struct student *s)
{
printf("student details are\n");
printf("%d%s%d",s->no,s->name,s->fee);
}
Q) Self-referential structures:
Self-referential structures are those structures that contain a reference to data of its same type.
A self-referential structure contains a pointer to a data that is of the same type as that of structures.
Q) Explain unions:
A union is a special data type available in C that allows to store different data types in the same
memory location.
Declaring a union:
A union is declared in the same way as a structure.
The syntax of union declaration is
union union_name
{
type element 1;
type element 2;
……………..
type element n;
};
Here union is the keyword, union_name is the user defined name for union, and type specifies any
valid data type. Element1,2…n are the members of union.
struct s Union u
{ {
int val; int val;
char cptr[10]; char cptr[10];
}s; }s;
The amount of memory required to store a unions, the amount of memory required is always
structure variable is the sum of the size of all the equal to that required by its largest member.
members.
In structure, each member have their own In union, one block is used by all the member of
memory space the union.
individual member can be accessed at a time Only one member can be accessed at a time.
9
Q) enumerated data types:
Enumerated data type is user defined data type.
The enumerated data type offers us a way of inventing our own data type.
An enumeration consists of a set of named integers. We assign symbolic names to integer
constants.
To define enumerated data types we use keyword “enum”.
Syntax:
enum enum_name
{
enumerator 1;
enumerator 2;
enumerator 3;
.
.
enumerator n;
};
Here enum is the keyword,enumerator1,2 ….n representing the integer constants 0,1,…n;
We can declare enum variable by
enum enum_name variable name;
Example:#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
enum week today;
today=wednesday;
printf("%d day",today+1);
getch();
return 0;
}
Example2:
#include< stdio.h>
void main()
{
int i;
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
clrscr();
for(i=JAN;i<=DEC;i++)
printf("\n%d",i);}
10