2. STRUCTURES
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.
3. Structure definition:
General format:
struct tag_name
{
datatype member1;
datatype member2;
…
…
};
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.
4. 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.
5. We can declare structure variables using the tag
name any where in the program.
For example the statement,
For example: struct lib_books book1,book2,book3;
declares book1,book2,book3 as variables of type
struct lib_books each declaration has four elements
of the structure lib_books.
6. struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
struct lib_books, book1, book2, book3;
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.
7. We can also combine both
template declaration and
variables declaration in one
statement.
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
} book1,book2,book3;
8. Giving values to members:
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 ‘.’ known as dot operator or period operator.
For example: Book1.price
scanf("%s",Book1.file);
scanf("%d",& Book1.pages);
strcpy(book1.title,"basic");
strcpy(book1.author,"Balagurusamy");
book1.pages=250;
book1.price=28.50;
9. Initializing structure:
Like other data type we can initialize structure when we
declare them.
As for initialization goes structure obeys the same set of rules
as arrays.
We initialize the fields of a structure by the following
structure declaration with a list containing values for each
fields as with arrays these values must be evaluate at compile
time.
10. struct student
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}newstudent;
struct student newstudent
{
12345,
“kapildev”
“pes college”;
“cse”;
19;
};
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.
11. UNION
A union is a special data type available in C that allows storing
different data types in the same memory location. You can define
a union with many members, but only one member can contain a
value at any given time. Unions provide an efficient way of using
the same memory location for multiple purposes.
Defining a Union:
To define a union, you must use the union statement in the same
way as you did while defining a structure. The union statement
defines a new data type with more than one member for your
program.
12. union [union name]
{
member definition;
member definition;
...
member definition;
};
union union_example
{
int integer;
float decimal;
char name[20];
};
The format of the union statement is as follows: