Académique Documents
Professionnel Documents
Culture Documents
Unit 9
Structure:
A structure is a collection of one or more variables, possibly of different types, grouped
together un-der a single name for convenient handling. (Structures are called ``records'' in
some languages, not-ably Pascal.) Structures help to organize complicated data, particularly
in large programs, because they permit a group of related variables to be treated as a unit
instead of as separate entities.
They are also called user defined data type.
One traditional example of a structure is the payroll record: an employee is described
by a set of attributes such as name, address, social security number, salary, etc. Some of
these in turn could be structures: a name has several components, as does an address and
even a salary. Anotherexample, more typical for C, comes from graphics: a point is a pair of
coordinate, a rectangle is a pair of points, and so on.
Declaration of Structure:
The general syntax for declaring a structure is:
struct structure_name
{
data_type member1;
data_type member2;
data_type member3;
data_type memberN;
};
We can define a structure to hold the information of a student as follows:
struct Student
{
char name[2];int roll;
char sec; float
marks;
};
Structure variable declaration:
structStudent s1, s2, s3;
We can combine both template declaration and structure variable declaration in one
statement. Eg,
struct Student
{
char name[2];
int roll;
char sec;
float marks;
} s1, s2, s3;
Accessing members of a structure:
There are two types of operators to access members of a structure. Which are:
Member operator (dot operator or period operator (.))
Structure pointer operator (->).
Structure initialization:
Like any data type, a structure variable can be initialized as follows:
struct Student
{
char name[20];int roll;
char sec; float
marks;
};
struct Student s1={Raju, 22, A, 55.5};
The s1 is a structure variable of type Student, whose members are assigned initial values.
The first member (name[20[) is assigned the string Raju, the second member (roll) is
assigned the integer value 22, the third member (sec) is assigned the character A, and the
fourth member (marks) is as-signed the float value 55.5.
Ashok Pandey
Page 1 of 7
Programming Language
Unit 9
Page 2 of 7
Programming Language
Unit 9
#include<stdio.h>
struct Student
{
char name[20];
int roll;
char sec;
float marks;
};
void main()
{
struct Student s[3];
int i;
for(i=0;i<3;i++)
{
printf(Enter the name of a student);
gets(s[i].name);
printf(Enter the roll number of a student);
scanf(%d,&s[i].roll);
printf(Enter the section of a student);
scanf(%c,&s[i].sec);
printf(Enter the marks obtained by the student);
scanf(%f,&s[i].marks);
}
//displaying the records
for(i=0;i<3;i++)
{
printf(Name=%s\n Roll number =%d\n Section=%c\nObtained marks=
%f,s[i].name, s[i].roll, s[i].sec, s[i].marks);
}
}
Arrays within structures:
C allow the use of array as member of structure.
Example:
#include<stdio.h>
struct Student
{
char name[20];
int roll;
char sec;
float marks[5];
};
void main()
{
struct Student s;
int i;
float sum=0, avg;
printf(Enter the name of a
student); gets(s[i].name);
printf(Enter the roll number of a student);
scanf(%d,&s[i].roll);
printf(Enter the section of a student);
scanf(%c,&s[i].sec);
printf(Enter the marks obtained by the student in 5 subjects);
for(i=0;i<5;i++)
{
scanf(%f,&s.marks[i]);
sum=sum+s.marks[i];
}
avg=sum/5;
//displaying the records
printf(Name=%s\n Roll number =%d\n Section=%c\nObtained marks=%f,
s.name, s.roll, s.sec, avg);
}
Structures within Structures:
Structures within structures mean nesting of structures. Study the following example and
understand the concepts.
Ashok Pandey
Page 3 of 7
Programming Language
Unit 9
Page 4 of 7
};
Programming Language
Unit 9
void main()
{
struct student s;
int i;
printf(Enter the name of a student);
gets(s.name);
printf(Enter age);
scanf(%d,&s.age);
printf(Enter the roll number of a student);
scanf(%d,&s.roll);
printf(Enter the section of a student);
scanf(%c,&s.sec);
display(s); //function call
}
void display(struct student st)
{
//displaying the records
printf(Name=%s\n Roll number =%d\n Age=%d\n Section=%c\n,st.name, st.roll,
st.age, st.sec);
}
Passing by reference:
This method uses the concept of pointer to pass structure as an argument. The address
location of the structure is passed to the called functions.
#include<stdio.h>
void display(struct student*);
struct student
{
char name[20];
int age;
int roll;
char sec;
};
void main()
{
struct student s;
inti;
printf(Enter the name of a student);
gets(s.name);
printf(Enter age);
scanf(%d,&s.age);
printf(Enter the roll number of a student);
scanf(%d,&s.roll);
printf(Enter the section of a student);
scanf(%c,&s.sec);
display(&s); //function call
}
void display(struct student *st)
{
//displaying the records
printf(Name=%s\n Roll number =%d\n Age=%d\n Section=%c\n,st->name,
st->roll, st->age, st->sec);
}
Typedef:
The typedef feature allow us to define new data types. Its purpose is to redefine the name
of an existing variable type. The general form of typedef is:
Typedef
data_type
new_type;
For example:
typedef int num;
typedef float real;
num i, j;
real a, b;
here, variables num and real acts as data types int and float respectively.
Ashok Pandey
Page 5 of 7
Programming Language
Unit 9
Unions:
A union is a variable that may hold (at different times) objects of different types and sizes,
with the compiler keeping track of size and alignment requirements. Unions provide a way to
manipulate different kinds of data in a single area of storage, without embedding any
machine-dependent information in the program.
Both structure and unions are used to group a number of different variables together.
Syntactically both structure and unions are exactly same. The main difference between them
is in storage. In structures, each member has its own memory location but all members of
union use the same memory location which is equal to the greatest members size.
Declaration of union:
The general syntax for declaring a union is:
union union_name
{
data_type member1;
data_type member2;
data_type member3;
data_type memberN;
};
We can define a union to hold the information of a student as
follows:
union Student
{
char name[2];
int roll;
char sec;
float marks;
};
union variable declaration:
union Student s1, s2, s3;
we can combine both template declaration & union variable declaration in one statement.
Eg,
union Student
{
char name[2];
int roll;
char sec;
float marks;
} s1, s2, s3;
Accessing members of a union:
Ashok Pandey
Page 6 of 7
Programming Language
Unit 9
There are two types of operators to access members of a union. Which are:
Member operator (dot operator or period operator (.))
union pointer operator (->).
union initialization:
Like any data type, a union variable can be initialized as follows:
union Student
{
char name[2];
int roll;
char sec;
float marks;
};
union Student s1={Raju, 22, A, 55.5};
The s1 is a union variable of type Student, whose members are assigned initial values. The
first member (name[20[) is assigned the string Raju, the second member (roll) is assigned
the integer value 22, the third member (sec) is assigned the character A, and the fourth
member (marks) is as-signed the float value 55.5.
Example: a program to assign some values to the member of union and to display
them on the screen.
#include<stdio.h>
union Sample
{
int x;
float y;
};
unionSample s;
void main()
{
s.x=55;
s.y=30.5;
printf(The
printf(The
}
Output:
The value of x is:
The value of y is:
Ashok Pandey
Page 7 of 7