0% found this document useful (0 votes)
3 views12 pages

UNIT 4 Structure

deco

Uploaded by

yoursketch000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

UNIT 4 Structure

deco

Uploaded by

yoursketch000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT -4

STRUCTURES

Structures:

The structure in C is a user-defined data type that can be used to


group items of possibly different types into a single type.
The struct keyword is used to define the structure in the C
programming language. The items in the structure are called
its member and they can be of any valid data type.

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:

1. Structure Variable Declaration with Structure Template

struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;

2. Structure Variable Declaration after Structure Template

// structure declared beforehand


struct structure_name variable1, variable2, .......;

Initialize Structure Members


Structure members cannot be initialized with the declaration.
For example, the following C program fails in the compilation.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members
here
int y = 0; // COMPILER ERROR: cannot initialize members
here
};
The reason for the above error is simple. When a datatype is
declared, no memory is allocated for it. Memory is allocated only
when variables are created.
We can initialize structure members in 3 ways which are as
follows:
1. Using Assignment Operator.
2. Using Initializer List.
3. Using Designated Initializer List.

1. Initialization using Assignment Operator

struct structure_name str;


str.member1 = value1;
str.member2 = value2;
str.member3 = value3;

2. Initialization using Initializer List

struct structure_name str = { value1, value2, value3 };


In this type of initialization, the values are assigned in sequential
order as they are declared in the structure template.

3. Initialization using Designated Initializer List

Designated Initialization allows structure members to be


initialized in any order. This feature has been added in the C99
standard.
struct structure_name str = { .member1 = value1, .member2 =
value2, .member3 = value3 };

Access Structure Members


We can access structure members by using the ( . ) dot
operator.

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. }

Nested Structure in C with Examples


A nested structure in C is a structure within structure. One
structure can be declared inside another structure in the same
way structure members are declared inside a structure.
Syntax:
struct name_1
{
member1;
member2;
.
.
membern;
struct name_2
{
member_1;
member_2;
.
.
member_n;
}, var1
} var2;
The member of a nested structure can be accessed using the
following syntax:
Variable name of Outer_Structure.Variable name of
Nested_Structure.data member to access
Different ways of nesting structure
The structure can be nested in the following different ways:
1. By separate nested structure
2. By embedded nested structure.
1. By separate nested structure: In this method, the two
structures are created, but the dependent structure(Employee)
should be used inside the main structure(Organisation) as a
member. Below is the C program to implement the approach:

// 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];

// Dependent structure is used


// as a member inside the main
// structure for implementing
// nested structure
struct Employee emp;
};
// Driver code
int main()
{
// Structure variable
struct Organisation org;

// Print the size of organisation


// structure
printf("The size of structure organisation : %ld\n",
sizeof(org));

org.emp.employee_id = 101;
strcpy(org.emp.name, "Robert");
org.emp.salary = 400000;
strcpy(org.organisation_name,
"GeeksforGeeks");
strcpy(org.org_number, "GFG123768");

// Printing the details


printf("Organisation Name : %s\n",
org.organisation_name);
printf("Organisation Number : %s\n",
org.org_number);
printf("Employee id : %d\n",
org.emp.employee_id);
printf("Employee name : %s\n",
org.emp.name);
printf("Employee Salary : %d\n",
org.emp.salary);
}

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

In this part, we only declare the template of the union, i.e., we


only declare the members’ names and data types along with the
name of the union. No memory is allocated to the union in the
declaration.
union union_name {
datatype member1;
datatype member2;
...
};
Keep in mind that we have to always end the union declaration
with a semi-colon.
Different Ways to Define a Union Variable

We need to define a variable of the union type to start using


union members. There are two methods using which we can
define a union variable.
1. With Union Declaration
2. After Union Declaration
1. Defining Union Variable with Declaration
union union_name {
datatype member1;
datatype member2;
...
} var1, var2, ...;

2. Defining Union Variable after Declaration


union union_name var1, var2, var3...;
where union_name is the name of an already declared union.

Access Union Members

We can access the members of a union by using the ( . ) dot


operator just like structures.
var1.member1;
where var1 is the union variable and member1 is the member
of the union.
The above method of accessing the members of the union also
works for the nested unions.
var1.member1.memberA;
Here,
 var1 is a union variable.
 member1 is a member of the union.
 memberA is a member of member1.

Initialization of Union in C

The initialization of a union is the initialization of its members by


simply assigning the value to it.
var1.member1 = some_value;
One important thing to note here is that only one member can
contain some value at a given instance of time.
Example of Union
// C Program to demonstrate how to use union
#include <stdio.h>

// union template or declaration


union un {
int member1;
char member2;
float member3;
};

// driver code
int main()
{

// defining a union variable


union un var1;

// initializing the union member


var1.member1 = 15;

printf("The value stored in member1 = %d",


var1.member1);

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

 // C Program to find the size of the union


 #include <stdio.h>

 // declaring multiple unions
 union test1 {
 int x;
 int y;
 } Test1;

 union test2 {
 int x;
 char y;
 } Test2;

 union test3 {
 int arr[10];
 char y;
 } Test3;

 // driver code
 int main()
 {
 // finding size using sizeof() operator
 int size1 = sizeof(Test1);
 int size2 = sizeof(Test2);
 int size3 = sizeof(Test3);

 printf("Sizeof test1: %d\n", size1);
 printf("Sizeof test2: %d\n", size2);
 printf("Sizeof test3: %d", size3);
 return 0;
 }

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

The size of the structure is equal to or The size of the union is


greater than the total size of all of its the size of its largest
members. member.

Only one member can


The structure can contain data in
contain data at the same
multiple members at the same time.
time.

It is declared using the


It is declared using the struct keyword.
union keyword.

You might also like