Vous êtes sur la page 1sur 7

Programming Language

Structure and union

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

Structure and union

Unit 9

Example: a program to assign some values to the member of structure and to


display them on the screen.
#include<stdio.h>
struct Sample
{
int x;
float y;
};
struct Sample s;
void main()
{
s.x=55;
s.y=30.5;
printf(The value of x is:%d\n, s.x);
printf(The value of y is:%d, s.y);
}
Output:
The value of x is: 55
The value of y is: 30.5
Example: program illustrates the structure in which read member elements of
structure and display them.
#include<stdio.h>
struct Student
{
char name[20];
int roll;
char sec;
float marks;
};
struct Student s1;
void main()
{
printf(Enter the name of a student); gets(s1.name);
printf(Enter the roll number of a
student); scanf(%d,&s1.roll);
printf(Enter the section of a student);
scanf(%c,&s1.sec);
printf(Enter the marks obtained by the student);
scanf(%f,&s1.marks); //
displaying the records
printf(Name=%s\n Roll number =%d\n Section=%c\n Obtained marks=%f,
s1.name, s1.roll, s1.sec, s1.marks);
}
Array of structures:
Like any array declaration, the array of structure variable declared in similar way and is
declared as follows:
#include<stdio.h>
struct Student
{
char name[20];int roll;
char sec; float
marks;
};
struct student s[50];
Here, s[50] is a structure variable. It can be accommodate the structure of a student up to
50. Each record may be accessed and processed separately like individual elements of an
array.
Example: this example declares an array of structure variable s having 3 members.
Each member in turn is a structure having four members.
Ashok Pandey

Page 2 of 7

Programming Language

Structure and union

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

Structure and union

Unit 9

Example: the following example shows a structure definition having another


structure as a member. In this example, person and students are two structures.
Person is used as a member of student.(person within Student)
#include<stdio.h>
Equivalent form of nested structure is:
struct Person
{
char name[20];
struct Student
int age;
{
int roll;
};
char sec;
struct Student
struct Person
{
int roll;
{
char name[20];
char sec;
int age;
struct Person p;
}p;
};
};
void main()
{
struct Student s;
printf(Enter the name of a student);
gets(s.p.name);
printf(Enter age);
scanf(%d,&s.p.age);
printf(Enter the roll number of a student);
scanf(%d,&s.roll);
printf(Enter the section of a student);
scanf(%c,&s.sec);
//displaying the records
printf(Name=%s\n Roll number =%d\n Age=%d\n Section=%c\n,s.p.name, s.roll,
s.p.age, s.sec);
}
Structure and functions:
C supports passing of structure as arguments to function. There are two methods by
which the values of a structure can be transferred from on function to another.
Passing by value
- Passing structure members to the function
- Passing entire structures to function
Passing by reference
Passing by value:
Passing a copy of structure member or entire structure to the called function. Any change to
the structure members within the function is not reflected in the original structure.
Passing structure member to functions:
#include<stdio.h>
void display(int, int);
struct sample
{
int x;
float y;
void main()
{
structsample s={33, 55.5};
display(s.x, s.y);
}
void display(int a, float b)
{
printf(%d\n%d,a, b);
}
Passing entire structures to functions:
#include<stdio.h>
void display(struct student);
struct student
{
char name[20];
int age;
int roll;
char sec;
};
Ashok Pandey

Page 4 of 7

};

Programming Language

Structure and union

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

Structure and union

Unit 9

Example: program show that the use of typedef


#include<stdio.h>
void display(int, int);
struct sample
{
int x;
float y;
};
void main()
{
typedef struct sample stvar;
stvar s={66, 87.9};
display(s.x, s.y);
}
void display(int a, float b)
{
printf(%d\n%d,a, b);

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

Structure and union

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:

value of x is:%d\n, s.x);


value of y is:%d, s.y);
55
30.5

Example: program illustrates the structure in which read member elements of


union and display them.
#include<stdio.h>
union Student
{
char name[20];
int roll;
char sec;
float marks;
};
union Student s1;
void main()
{
printf(Enter the name of a student); gets(s1.name);
printf(Enter the roll number of a student);
scanf(%d,&s1.roll);
printf(Enter the section of a student);
scanf(%c,&s1.sec);
printf(Enter the marks obtained by the student);
scanf(%f,&s1.marks);
//displaying the records
printf(Name=%s\n Roll number =%d\n Section=%c\n Obtained marks=
%f,s1.name, s1.roll, s1.sec, s1.marks);
}

Ashok Pandey

Page 7 of 7

Vous aimerez peut-être aussi