Ch-14-Structure Union Enumerated Data Types
Ch-14-Structure Union Enumerated Data Types
STRUCTURES
●stud1.DOB.dd = 15;
●stud1.DOB.mm = 09;
●stud1.DOB.yy = 1990;
●stud1.fees = 45000;
© Oxford University Press 2012. All rights reserved.
Write a program to read and display information of a student using
● #include<stdio.h> structure within a structure
● int main()
● { struct DOB
● {
● int day;
● int month;
● int year;
● };
● struct student
● { int roll_no;
● char name[100];
● float fees;
● struct DOB date;
● };
● struct student stud1;
● printf(“\n Enter the roll number : “);
● scanf(“%d”, &stud1.roll_no);
● printf(“\n Enter the name : “);
● scanf(“%s”, stud1.name);
● printf(“\n Enter the fees : “);
● scanf(“%f”, &stud1.fees);
● printf(“\n Enter the DOB : “);
● scanf(“%d %d %d”, &stud1.date.day, &stud1.date.month, &stud1.date.year);
● printf(“\n ********STUDENT’S DETAILS *******”);
● printf(“\n ROLL No. = %d”, stud1.roll_no);
● printf(“\n NAME. = %s”, stud1.name);
● printf(“\n FEES. = %f”, stud1.fees);
● printf(“\n DOB = %d - %d - %d”, stud1.date.day, stud1.date.month, stud1.date.year);
● }
● 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;
● #include<stdio.h>
● typedef struct
● {
● int x;
● int y;
● }POINT;
● void display(int, int);
● main()
● {
● POINT p1 = {2, 3};
● display(p1.x, p1.y);
● return 0;
● }
● void display( int a, int b)
● {
● printf("%d %d", a, b);
● }
© Oxford University Press 2012. 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. No doubt, this is a very inefficient method
especially when the structure is very big or the function is called frequently. Therefore, in such a
situation passing and working with pointers may be more efficient.
● 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);
● The code given below passes a structure to the function using call-by-value method.
● #include<stdio.h>
● typedef struct
● {
● int x;
● int y;
● }POINT;
● void display(POINT);
● main()
● {
● POINT p1 = {2, 3};
● display(p1);
● return 0;
● }
● void display( POINT p)
● {
● printf("%d %d", p.x, p.y);
● }
● Self referential structures are those structures that contain a reference to data of its same type.
That is, a self referential structure in addition to other data contains a pointer to a data that is of
the same type as that of the structure. For example, consider the structure node given below.
● struct node
● {
● int val;
● struct node *next;
● };
● Here the structure node will contain 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.
● Enumerated types can be implicitly or explicitly cast. For ex, the compiler can implicitly cast an
enumerated type to an integer when required.
● However, when we implicitly cast an integer to an enumerated type, the compiler will either
generate an error or warning message.
● To understand this, answer one question. If we write:
● enum COLORS{RED, BLUE, BLACK, GREEN, YELLOW, PURPLE, WHITE};
● enum COLORS c;c = BLACK + WHITE;
● Here, c is an enumerate data type variable. If we write, c = BLACK + WHITE, then logically, it
should be 2 + 6 = 8; which is basically a value of type int. However, the left hand side of the
assignment operator is of the type enum COLORS. SO the statement would complain an error.
● To remove the error, you can do either of two things. First, declare c to be an int.
● Second, cast the right hand side in the following manner. :
● c = enum COLORS(BLACK + WHITE);