Structure & Unions
Structure & Unions
Defining a Structure
To define a structure, you must use struct Books
the struct statement.
{
struct [structure tag]
{ member definition; char title[50];
member definition; char author[50];
... char subject[100];
member definition; int book_id;
} [one or more structure variables];
} book;
The structure 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 structure's definition,
before the final semicolon, you can specify one or
more structure variables but it is optional.
1
4/13/2022
Structure Initialization
struct Book struct Patient
{ {
char name[15]; float height;
int price; int weight;
int pages; int age;
} b1 , b2 ; };
b1.price=200; //b1 is variable of Book type and struct Patient p1 = { 180.75 , 73, 23 };
price is member of Book //initialization or,
We can also use scanf() to give values to struct patient p1;
structure members through terminal. p1.height = 180.75; //initialization of each
scanf(" %s ", b1.name); member separately
scanf(" %d ", &b1.price); p1.weight = 73;
p1.age = 23;
struct employee
Array of Structure {
char ename[10];
We can also declare an array of structure. Each
element of the array representing int sal;
a structure variable. }emp[5];
Example : struct employee emp[5]; void ask()
The above code define an array emp of size 5 {
elements. Each element of array emp is of int i,j;
type employee for(i=0;i<3;i++)
{
printf("\nEnter %d employee record\n",i+1);
printf("\nEmployee name\t");
scanf("%s",emp[i].ename);
2
4/13/2022
struct student
{
Passing structure by value char name[50];
int roll;
A structure variable can be passed to the };
function as an argument as normal variable. void Display(struct student std); /* function
If structure is passed by value, change made in prototype should be below to the structure declaration
structure variable in function definition does otherwise compiler shows error */
not reflect in original structure variable in int main()
calling function. {
struct student s1;
printf("Enter student's name: ");
scanf("%s",s1.name);
printf("Enter roll number:");
scanf("%d",&s1.roll);
Display(s1); // passing structure variable s1 as
argument
return 0; }
3
4/13/2022
struct distance
{
Passing structure by reference
int feet;
The address location of structure variable is float inch;
passed to function while passing it by reference. };
If structure is passed by reference, change made void Add(struct distance d1,struct distance d2, struct
in structure variable in function definition distance *d3);
reflects in original structure variable in the int main()
calling function. {
struct distance dist1, dist2, dist3;
printf("First distance\n");
printf("Enter feet: ");
scanf("%d",&dist1.feet);
printf("Enter inch: ");
scanf("%f",&dist1.inch);
printf("Second distance\n");
4
4/13/2022
Pointers to Structures
Output: struct Books *struct_pointer;
First distance Now, you can store the address of a structure
Enter feet: 12 variable in the above defined pointer variable.
To find the address of a structure variable,
Enter inch: 6.8
place the & operator before the structure's name
Second distance as follows:
Enter feet: 5 struct_pointer = &Book1;
Enter inch: 7.5 To access the members of a structure using a
Sum of distances = 18'-2.3“ pointer to that structure, you must use the ->
operator as follows:
struct_pointer->title;
The pointer variable of type struct name is Accessing structure member through pointer using
referenced to the address of p. Then, only the dynamic memory allocation
structure member through pointer can accessed. To access structure member using pointers, memory
Structure pointer member can also be accessed can be allocated dynamically using malloc()
using -> operator function defined under "stdlib.h" header file.
(*ptr).a is same as ptr->a Syntax to use malloc()
(*ptr).b is same as ptr->b ptr=(cast-type*)malloc(byte-size)
5
4/13/2022
Unions
Enter n: 2 A union is a special data type available in C
that enables you to store different data types in
Enter string, integer and floating number
the same memory location.
respectively:
You can define a union with many members, but
Programming 2 3.2 only one member can contain a value at any given
Enter string, integer and floating number time.
respectively: Like structure, Union in c language is a user
Structure 6 2.3 defined datatype that is used to hold different
type of elements.
Displaying Information
But it doesn't occupy sum of all members size. It
Programming 2 3.20 occupies the memory of largest member only. It
Structure 6 2.30 shares memory of largest member.
Defining a Union
Advantage of union over structure To define a union, you must use
It occupies less memory because it occupies the the union statement in very similar was as you
memory of largest member only. did while defining structure.
Disadvantage of union over structure The union statement defines a new data type, with
It can store data in one member only. more than one member for your program.
union [union tag]
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
6
4/13/2022
7
4/13/2022
union Data
{
int i;
float f; Output:
char str[20];
data.i : 10
};
data.f : 220.500000
int main( )
data.str : C Programming
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 220.5;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "C Programming");
printf( "data.str : %s\n", data.str);
return 0;
}
Output
size of union = 32
size of structure = 40
8
4/13/2022
union job
{
char name[32];
But, the memory required to store a union
variable is the memory required for largest float salary;
element of an union. int worker_no;
}u;
int main()
{
printf("Enter name:\n");
scanf("%s",&u.name);
printf("Enter salary: \n");
scanf("%f",&u.salary);
printf("Displaying\nName :%s\n",u.name);
printf("Salary: %f",u.salary);
return 0; }