Académique Documents
Professionnel Documents
Culture Documents
In this tutorial you will learn about C Programming - Structures and Unions, Giving
values to members, Initializing structure, Functions and structures, Passing structure to
elements to functions, Passing entire function to functions, Arrays of structure, Structure
within a structure and Union.
Arrays are used to store large set of data and manipulate them but the disadvantage is that
all the elements stored in an array are to be of the same data type. If we need to use a
collection of different data type items it is not possible using an array. When we require
using a collection of different data items of different data types we can use a structure.
Structure is a method of packing data of different types. A structure is a convenient
method of handling a group of related data items of different data types.
structure definition:
general format:
struct tag_name
{
data type member1;
data type member2;
…
…
}
Example:
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
the keyword struct declares a structure to holds the details of four fields namely title,
author pages and price. These are members of the structures. Each member may belong to
different or same data type. The tag name can be used to define objects that have the tag
names structure. The structure we just declared is not a variable by itself but a template
for the structure.
We can declare structure variables using the tag name any where in the program. For
example the statement,
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
structures do not occupy any memory until it is associated with the structure variable
such as book1. the template is terminated with a semicolon. While the entire declaration
is considered as a statement, each member is declared independently for its name and
type in a separate statement inside the template. The tag name such as lib_books can be
used to declare structure variables of its data type later in the program.
We can also combine both template declaration and variables declaration in one
statement, the declaration
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
} book1,book2,book3;
is valid. The use of tag name is optional for example
struct
{
…
…
…
}
A structure is usually defines before main along with macro definitions. In such cases the
structure assumes global status and all the functions can access the structure.
As mentioned earlier the members themselves are not variables they should be linked to
structure variables in order to make them meaningful members. The link between a
member and a variable is established using the member operator ‘.’ Which is known as
dot operator or period operator.
For example:
Book1.price
Is the variable representing the price of book1 and can be treated like any other ordinary
variable. We can use scanf statement to assign values like
scanf(“%s”,book1.file);
scanf(“%d”,& book1.pages);
strcpy(book1.title,”basic”);
strcpy(book1.author,”Balagurusamy”);
book1.pages=250;
book1.price=28.50;
Initializing structure:
Like other data type we can initialize structure when we declare them. As for initalization
goes structure obeys the same set of rules as arrays we initalize the fields of a structure by
the following structure declaration with a list containing values for weach fileds as with
arrays these values must be evaluate at compile time.
Example:
this initializes the id_no field to 12345, the name field to “kapildev”, the address field to
“pes college” the field combination to “cse” and the age field to 19.
We can pass structures as arguments to functions. Unlike array names however, which
always point to the start of the array, structure names are not pointers. As a result, when
we change structure parameter inside a function, we don’t effect its corresponding
argument.
in the declaration of structure type, emp_id and name have been declared as integer and
character array. When we call the function display() using
display(emp1.emp_id,emp1.name);
we are sending the emp_id and name to function display(0);
it can be immediately realized that to pass individual elements would become more
tedious as the number of structure elements go on increasing a better way would be to
pass the entire structure variable at a time.
# include stdio.h>
{
int emp_id;
char name[25];
char department[10];
float salary;
};
void main()
{
static struct employee emp1=
{
12,
“sadanand”,
“computer”,
7500.00
};
Arrays of structure:
structure information
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
student[100];
An array of structures can be assigned initial values just as any other array can.
Remember that each element is a structure that must be assigned corresponding initial
values as illustrated below.
struct date
{
int day;
int month;
int year;
};
struct student
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
structure date def;
structure date doa;
}oldstudent, newstudent;
the sturucture student constains another structure date as its one of its members.
Union:
Unions like structure contain members whose individual data types may differ from one
another. However the members that compose a union all share the same storage area
within the computers memory where as each member within a structure is assigned its
own unique storage area. Thus unions are used to observe memory. They are useful for
application involving multiple members. Where values need not be assigned to all the
members at any one time. Like structures union can be declared using the keyword union
as follows:
union item
{
int m;
float p;
char c;
}
code;
this declares a variable code of type union item. The union contains three members each
with a different data type. However we can use only one of them at a time. This is
because if only one location is allocated for union variable irrespective of size. The
compiler allocates a piece of storage that is large enough to access a union member we
can use the same syntax that we use to access structure members. That is
code.m
code.p
code.c
are all valid member variables. During accessing we should make sure that we are
accessing the member whose value is currently stored.
For example a statement such as
code.m=456;
code.p=456.78;
printf(“%d”,code.m);
In effect a union creates a storage location that can be used by one of its members at a
time. When a different number is assigned a new value the new value supercedes the
previous members value. Unions may be used in all places where a structure is allowed.
The notation for accessing a union member that is nested inside a structure remains the
same as for the nested structure.