Structures allow grouping of related data and can be used to represent records. A structure defines a template for the format of its members. Structures can contain basic data types and arrays. Structure variables can be initialized, and members accessed using dot operator. Arrays of structures can be used to represent tables of related data. Unions share the same storage location for members, allowing only one member to be active at a time. Both structures and unions can be used as function parameters.
Introduction to structures as data types for handling logically related data items, defining structures, accessing members using dot operator, and initializing structures.
Introduction to unions, their syntax, memory storage differences compared to structures, and how to declare and use union members.
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 definationcreates 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:
1. The templateterminated 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.
The keywordstruct 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’.
The linkbetween 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 ishow 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 Structuremust 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 thereare 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 usestructure 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 elementis 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 permitsthe 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 mainphilosophy 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 ofa 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 area 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 maycontain 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;
A unionvariable 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 membersshare the same storage area in
computers memory.