UNIT-IV
STRUCTURE
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.
Structures are used to represent a record .
Defining a structure:
To define a structure, must use the struct statement.
The struct statement defines a new data type, with more than or equal to one
member.
The items in the structure are called its member and they can be of any valid data
type.
The format of the struct statement is as follows:
struct [structure name]
{
member definition;
member definition;
...
member definition;
};
(OR)
struct [structure name]
{
member definition;
member definition;
...
member definition;
}structure variable declaration;
C Structure Declaration
To declare structure in C before using it in our program.
In structure declaration, specify its member variables along with their datatype.
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;
....
....
};
Access Structure Members
To access structure members by using the ( . ) dot operator.
Syntax
structure_name.member1;
strcuture_name.member2;
Initialize Structure Members
Structure members cannot be initialized with the declaration.
When a datatype is declared, no memory is allocated for it.
Memory is allocated only when variables are created.
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.
struct structure_name str = { .member1 = value1, .member2 = value2, .member3 =
value3 };
Example of Structure in C
#include <stdio.h>
struct str1
{
int i;
char c;
float f;
char s[30];
};
struct str2 {
int ii;
char cc;
float ff;
} var;
int main()
{
struct str1 var1 = { 1, 'A', 1.00, "IT" },
var2;
struct str2 var3 = { .ff = 5.00, .ii = 5, .cc = 'a' };
var2 = var1;
printf("Struct 1:\n\ti = %d, c = %c, f = %f, s = %s\n", var1.i, var1.c, var1.f, var1.s);
printf("Struct 2:\n\ti = %d, c = %c, f = %f, s = %s\n", var2.i, var2.c, var2.f, var2.s);
printf("Struct 3\n\ti = %d, c = %c, f = %f\n", var3.ii, var3.cc, var3.ff);
return 0;
Output
Struct 1:
i = 1, c = A, f = 1.000000, s = IT
Struct 2:
i = 1, c = A, f = 1.000000, s = IT
Struct 3
i = 5, c = a, f = 5.000000
Array of Structures in C
An array of structres in C can be defined as the collection of multiple structures
variables where each variable contains information about different entities.
The array of structures in C are used to store information about multiple entities of
different data types.
The array of structures is also known as the collection of structures.
EXAMPLE
#include<stdio.h>
#include <string.h>
struct student{
int rollno;
char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 students");
for(i=0;i<5;i++)
{
printf("\nEnter Rollno:");
scanf("%d",&st[i].rollno);
printf("\nEnter Name:");
scanf("%s",&st[i].name);
}
printf("\nStudent Information List:");
for(i=0;i<5;i++)
{
printf("\nRollno:%d, Name:%s",st[i].rollno,st[i].name);
}
return 0;
}
Output:
Enter Records of 5 students
Enter Rollno:1
Enter Name:Sonoo
Enter Rollno:2
Enter Name:Ratan
Enter Rollno:3
Enter Name:Vimal
Enter Rollno:4
Enter Name:James
Enter Rollno:5
Enter Name:Sarfraz
Student Information List:
Rollno:1, Name:Sonoo
Rollno:2, Name:Ratan
Rollno:3, Name:Vimal
Rollno:4, Name:James
Rollno:5, Name:Sarfraz
ARRAY WITHIN A STRUCTURE
An array of structure in C programming is a collection of different datatype variables,
grouped together under a single name.
General form of structure declaration
The structural declaration is as follows −
struct tagname{
datatype member1;
datatype member2;
datatype member n;
};
Here, struct is the keyword.
tagname specifies the name of structure.
member1, member2 specifies the data items that make up structure.
Example
struct book{
int pages;
char author [30];
float price;
};
Example
#include <stdio.h>
struct candidate
{
int roll_no;
char grade;
float marks[4];
};
void display(struct candidate a1)
{
printf("Roll number : %d", a1.roll_no);
printf("Grade : %c", a1.grade);
printf("Marks secured: ");
int i;
int len = sizeof(a1.marks) / sizeof(float);
for (i = 0; i < len; i++)
{
printf("Subject %d : %.2f", i + 1, a1.marks[i]);
}
}
int main()
{
struct candidate A= { 1, 'A', { 98.5, 77, 89, 78.5 } };
display(A);
return 0;
}
Output
Roll number : 1
Grade : A
Marks secured:
Subject 1 : 98.50
Subject 2 : 77.00
Subject 3 : 89.00
Subject 4 : 78.50
UNION IN C
Union can be defined as a user-defined data type which is a collection of different
variables of different data types in the same memory location.
The union can also be defined as many members, but only one member can contain a
value at a particular point in time.
The keyword union is used to declare the union in C.
Variables inside the union are called members of the union.
Defining a Union in C
Syntax:
union unionName
//member definitions
};
Example:
union Courses
char WebSite[50];
char Subject[50];
int Price;
};
Accessing Union Members in C
Example:
#include<stdio.h>
#include<string.h>
union Courses
char WebSite[50];
char Subject[50];
int Price;
};
void main( )
union Courses C;
strcpy( C.WebSite, "w3schools.in");
printf( "WebSite : %s\n", C.WebSite);
strcpy( C.Subject, "The C Programming Language");
printf( "Book Author : %s\n", C.Subject);
C.Price = 0;
printf( "Book Price : %d\n", C.Price);
Program Output:
WebSite : w3schools.in
Book Author : The C Programming Language
Book Price : 0