UNIT 4 Structure
UNIT 4 Structure
STRUCTURES
Structures:
Structure Declaration
We have to declare structure in C before using it in our program.
In structure declaration, we specify its member variables along
with their datatype. We can use the struct keyword to declare
the structure in C using the following syntax:
Syntax
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
The above syntax is also called a structure template or structure
prototype and no memory is allocated to the structure in the
declaration.
Structure Definition
To use structure in our program, we have to define its instance.
We can do that by creating variables of the structure type. We
can define structure variables using two methods:
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
Syntax
structure_name.member1;
strcuture_name.member2;
In the case where we have a pointer to the structure, we can also
use the arrow operator to access the members.
EXAMPLE OF STRUCTURE:-
1. #include <string.h>
2. struct employee
3. { int id;
4. char name[50];
5. float salary;
6. }e1,e2; //declaring e1 and e2 variables for structure
7. int main( )
8. {
9. //store first employee information
10. e1.id=101;
11. strcpy(e1.name, "Sonoo Jaiswal");//copying string into char a
rray
12. e1.salary=56000;
13.
14. //store second employee information
15. e2.id=102;
16. strcpy(e2.name, "James Bond");
17. e2.salary=126000;
18.
19. //printing first employee information
20. printf( "employee 1 id : %d\n", e1.id);
21. printf( "employee 1 name : %s\n", e1.name);
22. printf( "employee 1 salary : %f\n", e1.salary);
23.
24. //printing second employee information
25. printf( "employee 2 id : %d\n", e2.id);
26. printf( "employee 2 name : %s\n", e2.name);
27. printf( "employee 2 salary : %f\n", e2.salary);
28. return 0;
29. }
Output:
employee 1 id : 101
employee 1 name : Sonoo Jaiswal
employee 1 salary : 56000.000000
employee 2 id : 102
employee 2 name : James Bond
employee 2 salary : 126000.000000
EXAMPLE
1. #include<stdio.h>
2. void main ()
3. {
4. char names[2][10],dummy; // 2-dimensioanal character array nam
es is used to store the names of the students
5. int roll_numbers[2],i;
6. float marks[2];
7. for (i=0;i<3;i++)
8. {
9.
10. printf("Enter the name, roll number, and marks of the stude
nt %d",i+1);
11. scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);
12. scanf("%c",&dummy); // enter will be stored into dummy ch
aracter at each iteration
13. }
14. printf("Printing the Student details ...\n");
15. for (i=0;i<3;i++)
16. {
17. printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);
18. }
19. }
// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
// Declaration of the
// dependent structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
// Declaration of the
// Outer structure
struct Organisation
{
char organisation_name[20];
char org_number[20];
org.emp.employee_id = 101;
strcpy(org.emp.name, "Robert");
org.emp.salary = 400000;
strcpy(org.organisation_name,
"GeeksforGeeks");
strcpy(org.org_number, "GFG123768");
Output:
The size of structure organisation : 68
Organisation Name : GeeksforGeeks
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000
Unions
The Union is a user-defined data type in C language that can
contain elements of the different data types just like structure.
But unlike structures, all the members in the C union are stored in
the same memory location. Due to this, only one member can
store data at the given instance.
Syntax of Union in C
The syntax of the union in C can be divided into three steps
which are as follows:
C Union Declaration
Initialization of Union in C
// driver code
int main()
{
return 0;
}
Output
The value stored in member1 = 15
Size of Union
The size of the union will always be equal to the size of the
largest member of the array. All the less-sized elements can
store the data in the same space without any overflow.
C program to find the size of the union
C
Output
Sizeof test1: 4
Sizeof test2: 4
Sizeof test3: 40
Difference between C Structure and C
Union
The following table lists the key difference between the structure
and union in C:
Structure Union