Data Structures Using C, 2e Reema Thareja
Data Structures Using C, 2e Reema Thareja
C, 2e
Reema Thareja
stud2 = stud1;
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.