Struct
Struct
A structure is a user defined data type in C/C++. A structure creates a data type
that can be used to group items of possibly different types into a single type.
Keyword “struct” is used to create a structure.
struct address
{
// members or fields of structure
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
Structure members can be initialized using curly braces ‘{}’. Structure members
are accessed using dot (.) operator.
#include <stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = { 4, 7 };
printf("x = %d, y = %d\n", p1.x, p1.y);
struct Point
{
int x, y;
};
int main()
{
// Create an array of structures
struct Point arr[5];
struct Point
{
int x, y;
};
int main()
{
Point *p1 = new Point;
p1->x = 10; p1->y = 23;
printf("%d %d\n", p1->x, p1->y);
struct Point
{
int x, y;
};
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
int main()
{
printf("%d\n", sizeof(Point));
printf("%d\n", sizeof(address));
return 0;
}
This program explains how to use structure within structure in C using normal
variable. “student_college_detail” structure is declared inside “student_detail” structure
in this program. Both structure variables are normal structure variables.
#include <stdio.h>
struct student_college_detail
{
int college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detail clg_data;
} stu_data;
int main()
{
struct student_detail stu_data = { 1, "Raju", 90.5, 71145,
"Anna University" };
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n", stu_data.percentage);
E-OLYMP 4817. Rectangle Find the perimeter and the area of a rectangle.
► Use struct rectangle to solve the problem.
#include <stdio.h>
struct rectangle
{
int x, y;
} a;
int main(void)
{
while (scanf("%d %d", &a.x, &a.y) == 2)
printf("%d %d\n", GetPerimeter(a), GetArea(a));
return 0;
}
E-OLYMP 972. Sorting time Sort the time according to specified criteria.
► Declare the time structure that contains hours, minutes and seconds.
struct MyTime
{
int hour, min, sec;
};
If hours and minutes are equal, sort the time in increasing order of seconds.
if ((a.hour == b.hour) && (a.min == b.min)) return a.sec < b.sec;
If hours are equal (minutes are not equal), sort the time in increasing order of
minutes.
if (a.hour == b.hour) return a.min < b.min;
Union
A union is a special data type available in C that allows to store different data
types in the same memory location. You can define a union with many members, but
only one member can contain a value at any given time. Unions provide an efficient
way of using the same memory location for multiple-purpose.
To define a union, you must use the union statement in the same way as you did
while defining a structure. The union statement defines a new data type with more than
one member for your program. The format of the union statement is as follows −
union[union tag]
{
member definition;
member definition;
...
member definition;
}[one or more union variables];
The union tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At the end of
the union’s definition, before the final semicolon, you can specify one or more union
variables but it is optional. Here is the way you would define a union type named Data
having three members:
#include <stdio.h>
union Data
{
int i;
float f;
char str[20];
};
int main()
{
union Data data;
printf("Memory size = %d\n", sizeof(data));
return 0;
}
To access any member of a union, we use the member access operator (.). The
member access operator is coded as a period between the union variable name and the
union member that we wish to access.
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main()
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy(data.str, "C Programming");
return 0;
}
When the above code is compiled and executed, it produces the following result
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming
Here, we can see that the values of i and f members of union got corrupted because
the final value assigned to the variable has occupied the memory location and this is the
reason that the value of str member is getting printed very well.
Now let’s look into the same example once again where we will use one variable at
a time which is the main purpose of having unions −
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main()
{
union Data data;
data.i = 10;
printf("data.i : %d\n", data.i);
data.f = 220.5;
printf("data.f : %f\n", data.f);
return 0;
}
When the above code is compiled and executed, it produces the following result −
data.i : 10
data.f : 220.500000
data.str : C Programming
Here, all the members are getting printed very well because one member is being
used at a time.