0% found this document useful (0 votes)
2K views20 pages

Data Structures Using C, 2e Reema Thareja

Uploaded by

seravanakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views20 pages

Data Structures Using C, 2e Reema Thareja

Uploaded by

seravanakumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Structures Using

C, 2e

Reema Thareja

© Oxford University Press 2014. All rights reserved.


Chapter 5

Structures and Unions

© Oxford University Press 2014. All rights reserved.


Introduction
• Structure is a user-defined data type that can store related
information (even of different data types) together.
• A structure is declared using the keyword struct followed by a
structure name.
• All the variables of a structure are declared within the structure.
• A structure type is defined by using the following syntax:
struct struct-name
{ data_type var-name;
data_type var-name;
...
};
© Oxford University Press 2014. All rights reserved.
Introduction
struct student
{ int r_no;
char name[20];
};
• The structure definition does not allocate any memory.
• It just gives a template that conveys to the compiler how the
structure is laid out in memory and gives details of the members.
• Memory is allocated for the structure when we declare a variable
of the structure.
• For example, we can define a variable of student by writing
struct student stud1;
© Oxford University Press 2014. All rights reserved.
Typedef Declaration
• When we precede a struct name with typedef keyword, then the
struct becomes a new type.
• For example, consider the following declaration:
typedef struct student
{
int r_no;
char name[20];
};
• Now we can straightaway declare variables of this new data type
as we declare variables of type int, float, char, double, etc.
• To declare a variable of structure student, we will just write:
student stud1;
© Oxford University Press 2014. All rights reserved.
Initializing Structures
• Initializing a structure means assigning some constants to the
members of the structure.
• When the user does not explicitly initializes the structure then C
automatically does that.
• For int and float members, the values are initialized to zero and
char and string members are initialized to ‘\0’ by default.
• The initializers are enclosed in braces and are separated by
commas.
• Note that initializers should match their corresponding types in
the structure definition.

© Oxford University Press 2014. All rights reserved.


Initializing Structures
• The general syntax to initialize a structure variable is given as
follows:
struct struct_name
{
data_type member_name 1;
data_type member_name 2;
data_type member_name 3;
…………………………………………..
}struct_var= {constant1, constant2, constant3, ….};

© Oxford University Press 2014. All rights reserved.


Accessing the Members of a Structure
• Each member of a structure can be used just like a normal
variable, but its name will be a bit longer.
• A structure member variable is generally accessed using a ‘.’ (dot
operator).
• The syntax of accessing a member of a structure is:
struct_var.member_name
• For example, to assign value to the individual data members of
the structure variable stud1, we may write:
stud1.r_no = 01;

© Oxford University Press 2014. All rights reserved.


Accessing the Members of a Structure

• We can assign a structure to another structure of the same type.

• For example, if we have two structure variables stud1 and stud2

of type struct student

struct student stud1 = {01, "Rahul", "BCA", 45000};

struct student stud2;

• Then to assign one structure variable to another, we will write:

stud2 = stud1;

© Oxford University Press 2014. All rights reserved.


Nested Structures
• A structure can be placed within another structure.
• Such a structure that contains another structure as its member is
called a nested structure.
typedef struct typedef struct
typedef struct
{ char first_name[20]; { int dd;
{ int r_no;
char mid_name[20]; int mm;
NAME name;
char last_name[20]; int yy;
DATE DOB;
} NAME; }DATE;
}student;
To assign values to the structure fields, we will write:
struct student stud1;
stud1.name.first_name = “Janak”;
stud1.DOB.dd = 15;
stud1.DOB.mm = 03;
stud1.DOB.yy= 1990;
© Oxford University Press 2014. All rights reserved.
Arrays of Structures
• The general syntax for declaring an array of structure can be
given as:
struct struct_name struct_var[index];
struct student stud[30];
• Now, to assign values to the ith student of the class, we will write:
stud[i].r_no = 09;
stud[i].name = “RASHI”;
stud[i].course = “MCA”;
stud[i].fees = 60000;

© Oxford University Press 2014. All rights reserved.


Passing Individual Structure Members to a
Function
• To pass any individual member of the structure to a function we
must use the direct selection operator to refer to the individual
members for the actual parameters.
• The called program does not know if the two variables are
ordinary variables or structure members.
typedef struct
{ int x;
int y;
}POINT;
POINT p1={2,3};
display(p1.x, p1.y); //passing members of p1 to function display()
© Oxford University Press 2014. All rights reserved.
Passing a Structure to a Function
• When a structure is passed as an argument, it is passed using call
by value method. That is a copy of each member of the structure
is made.
• The general syntax for passing a structure to a function and
returning a structure can be given as:
struct struct_name func_name(struct struct_name struct_var);

typedef struct
{ int x;
int y;
}POINT;
POINT p1={2,3};
display(p1); //passing entire structure p1 to function display()
© Oxford University Press 2014. All rights reserved.
Passing Structures through Pointers
• C allows to create a pointer to a structure.
• Like in other cases, a pointer to a structure is never itself a
structure, but merely a variable that holds the address of a
structure.
• The syntax to declare a pointer to a structure can be given as:
struct struct_name
{
data_type member_name1;
data_type member_name2;
.....................................
}*ptr;
OR
struct struct_name *ptr;
© Oxford University Press 2014. All rights reserved.
Passing Structures through Pointers
• For our student structure we can declare a pointer variable by
writing:
struct student *ptr_stud, stud;
• The next step is to assign the address of stud to the pointer
using the address operator (&). So to assign the address, we will
write:
ptr_stud = &stud;
• To access the members of the structure, one way is to write:
(*ptr_stud).roll_no;
• An alternative to the above statement can be used by using
‘pointing-to’ operator (->):
ptr_stud->roll_no = 01;
© Oxford University Press 2014. All rights reserved.
Self-referential Structures
• Self-referential structures are those structures that contain a
reference to data of its same type.
• That is, a self-referential structure contains a pointer to a data
that is of the same type as that of the structure.
struct node
{ int val;
struct node *next;
};
• Here the structure node contains two types of data: an integer
val and next that is a pointer to a node. You must be wondering
why do we need such a structure? Actually, self-referential
structure is the foundation of other data structures.
© Oxford University Press 2014. All rights reserved.
Unions
• Similar to structures, a union is a collection of variables of
different data types.
• The difference between a structure and a union is that in
unions, information can be stored in one field at any one time.
• Unions are used to save memory. They are useful for
applications
that involve multiple members, where values need not be
assigned to all the members at any one time.
• The syntax for union declaration can be given as :
union union-name
{
data_type var-name;
data_type var-name;
................................
}; © Oxford University Press 2014. All rights reserved.
Initializing Unions
#include <stdio.h>
typedef union POINT2
{
int x;
int y;
};
int main()
{
POINT2 P2;
P2. x = 4;
printf(“\n The x coordinate of P2 is %d”, P2.x);
P2.y = 5;
printf(“\n The y coordinate of P2 is %d”, P2.y);
return 0;
} © Oxford University Press 2014. All rights reserved.
Arrays of Unions
#include <stdio.h>
union POINT
{ int x, y;
};
int main()
{ int i;
union POINT points[3];
points[0].x = 2; points[0].y = 3; points[1].x =
4;
points[1].y = 5; points[2].x = 6; points[2].y = 7;
for(i=0;i<3;i++)
printf(“\n Coordinates of Point[%d] are %d and %d”, i,
points[i].x, points[i].y);
return 0;
} © Oxford University Press 2014. All rights reserved.
Unions inside Structures
#include <stdio.h>
struct student
{ union
{ char name[20];
int roll_no;
};
int marks;
};
/* in main() function*/
printf(“\n You can enter the name or roll number of the student”);
printf(“\n Do you want to enter the name? (Y or N): ”);
gets(choice);
if(choice==‘y’ || choice==‘Y’)
{ printf(“\n Enter the name: ”);
gets(stud.name);
}
else
{ printf(“\n Enter the roll number: ”);
scanf(“%d”, &stud.roll_no);
} © Oxford University Press 2014. All rights reserved.

You might also like