2. 1. INTRODUCTION
C’ Supports a constructed data type known
as STRUCTURE, Which is a method of
packing data of different types.
WHAT IS STRUCTURE ?
A Structure is a convenient tool for handling
a group of logically related data items.
i.e. It can be used to represent a set of
attributes, such as : student_name , roll_no
3. A structure defination creates a
formet that may be used to declare
structure variables.
i.e. Consider a book database
consisting of a book name, Author,
Number of pages and price.
We can define a structure to hold this
information as follows:
5. 1. The template terminated with a semicolon
2. While the entire declarationn considered as a
satement, each member is declared
indepandently for its name and type in a
separate statement inside the teplate.
3. The tag name such as book_bank can be
used to declare structure variables of its
type,letter in the program.
7. The keyword struct declares a structure to
hold the details of four fields, namely
title,author,pages and price.
These fields are called structure elements or
members and each member belong to
different type of data.
Book_bank is the name of the structure and
also called ‘STRUCTURE TAG’.
9. The link between a member and variable is
established using the member operator ‘.’
which is also known as ‘dot operator’ or
‘period operator’.
i.e.
book1.price
Is the variable represnting the price of book1
and can be treated like any other ordinary
variable.
10. Here is how we would assign values to the
members of book1:
strcpy(book1.title, “COMPUTER”);
strcpy(book1.author, “XYZ”);
book1.pages=250;
book1.price=29.99;
We can also use scanf to give the values
through keyboard.
11. A Structure must be declared as static if it is
to be initialized inside a function.
main()
{
static struct
{
int weight;
float height;
}
student = (60,180.75);
…..
}
12. If there are fewer initialization than that of
member variables in the structure. The
remaining member variables are initialized to
zero.
i.e. if we don’t know the number of pages in
book:
struct book b1={“let us
C”,”kanetkar”,0,150.50};
13. We use structure todescribe the format of a
number related variables.
In such cases, we may declare array of
structures,each element of an array represent
a structure variable
i.e. struct class student[100]
This defines an array called student , that
consists of 100 element.
14. Each element is defined to be of the type
struct class.
An array of structure is stored inside the
memory in the same way as a multi-
dimensional array.
15. C permits the use of array as a structure
members.
We can use single or multi-dimensional array
of type int or float.
i.e. Struct marks
{
int number;
float subject[3];
} student[2];
16. The main philosophy of c language is the use
of functions. C supports the passing of
structure values as arguments to function.
There are 3 methods by which the values of a
structure can be transfferd from one function
to another:
1. To pass each member of the structure as an
argument of function call.
17. 2. Passing of a copy of the entire structure to
the called function
3. Pointers to pass the structure as an
argument.
General formet of sending a copy of a
structure to thr called function:
data_type function name(st_name)
srtuct_type st_name;
{
…
return(expression);
}
18. Unions are a concept borrowed from
structures and therefore follow the same
syntax as structures.
Major diffferance in terms of Storage:
In structures each member has its
own storage location
In unions all members use the same
location
19. Union may contain many members of
different type but can handle only one
member at a time.
It can be declared using keyword union as
follows:
union item
{
int m;
float x;
char c;
} code;
22. A union variable can be assigned to another
union variable.
Address of the union variable is obtained
using the address of ‘&’ operator.
It is to pass a union to function and a
function can return a union.
We can use pointer to unions and within
unions.
23. All members share the same storage area in
computers memory.