Structures:
It is a collection of heterogeneous elements of different data
type.
Defining the Structure:
The general format of structure is given below:
Declaring A Structure Variable: Ways To Declare Structure Variable.
(Model Question paper 2025)
We can declare variables of the structure name in the same way as we declare variables of
predefined data type.
Syntax: struct structure_name variable_name;
Example:
struct student
{
int roll_no, age; Structure template with
tag
char name[20], grade; Here student is the
tag
float collg_fee;
};
struct student student1; /*student1 is structure variable
We can also declare multiple structure variables using a single line of code by using
following syntax:
Syntax:
struct structure_type variable1,variable2,………….variablen;
Example: struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
struct student student1,student2;
The definition of a structure and declaration of structure variables can be done
together.
Syntax:
struct structure_name
{
data_type1 variable1;
data_type2 variable2;
…………………………
…………………………
data_type n variablen;
} structure_variable1, structure_variable2,………, structure_variablen;
Example: struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
}student1,student2;
Structure name is student and we have declared two variables of type student: student1
and student2.
Accessing Structure Members:
A member of a structure is identified and accessed using the (.) Dot Operator. The
Dot Operator connects the member name to the name of its containing Structure.
Syntax:
structure_variable_name.member;
Example: student1.roll_no;
[Link];
Initializing Structure variable:
Each structure variable contains a copy of all the members of the structure.
Initializing a structure variable involves assigning values to its structure members.
We can assign values to the members of the structure variables either all at once or one at
a time.
i. In first approach, the declaration of the structure variable and the initialization
of the structure members are done using a single line of code.
Example: struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
struct student student1={50,18, “Rama”, ‘A’,35500.00};
ii. In the second approach, we first need to declare the structure variables and then
initialize each member of the structure variable one by one.
Initializing each structure members separately requires us to access individually structure
members. We can access a structure member by dot (.) operator.
Syntax : structure_variable_name.member_name=value;
structure_name: It represents the name of the Structure variable.
member_name: It represents a member of the Structure variable.
value: It represents the value being assigned to the Structure member.
Example: struct student
{
int roll_no, age;
char name[20],grade;
float collg_fee;
} student1;
student1.roll_no=50;
[Link]=18;
[Link]= “Rama”;
[Link]= ‘A’;
student1.collg_fee=35500.00;
Example: Write a C program to print the details of student using structure.
#include <stdio.h>
#include<conio.h>
struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
void main( )
{
struct student student1={50,18, “Rama”, ‘A’,35500.00};
clrscr();
printf(“Student Details:\n”);
printf(“Roll Number:%d\n”,student1.roll_no);
printf(“Age:%d\n”,[Link]);
printf(“Name:%s\n”,[Link]);
printf(“Grade:%c\n”,[Link]);
printf(“College Fee:%f\n”,student1.collg_fee);
getch();
}
Structure Assignment:
Comparing Structures:(MODEL QP 2025)
Array Of Structures:
Similar to creating arrays of a predefined data type such as int, float and char, we can also
create an array of structure type.
Arrays of Structure type are required in situations when we need to apply the same
Structure to a set of Objects.
Example: Defining a Structure and then creating an array of Structure type:
struct student
{
int roll_no,age;
char name[10],grade;
float collg_fee;
}students[20];
Here we have created a structure called ‘student’ with 5 fields: roll_no, age, name, grade
and collg_fee and array of structure type of size 20(We can store the details of 20
students).
Example:
1. Write a C program to print the details of students using array of structures.
#include<stdio.h>
#include<conio.h>
struct student
{
int roll_no,age;
char name[10],grade;
float collg_fee;
}students[20];
void main()
{
int i;
printf(“Enter the details of 3 Students:\n”); OUTPUT:
for(i=0;i<3;i++) Enter the details of 3 Students:
{ Enter the Roll_no:101
printf(“Enter the Roll_no:”); Enter the age: 18
scanf(“%d”,&students[i].roll_no); Enter the Name: manju
printf(“Enter the age:”); Enter the Grade: A
scanf(“%d”,&students[i].age); Enter the College fee: 35500
printf(“Enter the Name:”);
scanf(“%s”,students[i].name); Enter the Roll_no:102
printf(“Enter the Grade:”); Enter the age: 18
scanf(“%c”,students[i].grade); Enter the Name: raju
printf(“Enter the College fee:”); Enter the Grade: B
scanf(“%f”,&students[i].collg_fee); Enter the College fee: 35500
}
printf(“Student details are:\n”); Enter the Roll_no:103
Enter the age: 18
for (i=0;i<3;i++) Enter the Name: ravi
{ Enter the Grade: C
printf(“Roll Number:%d\n”,students[i].roll_no); Enter the College fee: 35500.00
Student details are:
Roll Number: 101
Age: 18
Name: manju
Grade: A
printf(“Age:%d\n”,students[i].age);
printf(“Name:%s\n”,students[i].name);
printf(“Grade:%c\n”,students[i].grade);
printf(“College fee:%f\n”,students[i].collg_fee);
}
getch();
}
Passing Structures to Functions:
Passing Entire Structures to Functions:
We can pass an entire structure as a parameter to a function instead of passing
individual structure members.
Structures are passed by value and enable a function to alter a parameter and return a
value as structure.
Function definition must consist of declaration of a Structure and Structure variable as its
formal parameter.
The Structure variables used in both calling function (actual parameter) and in called
function (formal parameter) belong to same Structure name or Structure template.
Example: Write a C program to pass a structure as a parameter to a function.
#include <stdio.h>
#include<conio.h>
struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
void main ( )
{
struct student st;
printf(“Enter the student Details:\n”);
printf(“Enter the student Roll_no:”);
scanf(“%d”,&st.roll_no); OUTPUT:
printf(“Enter the student Age:”); Enter the student Details:
scanf(“%d”,&[Link]); Enter the student Roll_no: 10
printf(“Enter the student Name:”); Enter the student Age: 18
scanf(“%s”,[Link]); Enter the student Name: Rama
printf(“Enter the student Grade:”); Enter the student Grade: A
scanf(“%c”,[Link]); Enter the College fee: 35500
printf(“Enter the College fee:”); Student details:
scanf(“%f”,&st.collg_fee); Roll_no: 10
print_details(st); Age: 18
getch(); Name: Rama
} Grade: A
void print_details(st) College fee: 35500.00
{
printf(“Student details:\n”);
printf(“Roll_no:%d\n Age:%d\n Name:%s \n Marks:%d\n College fee:
%f”,st.roll_no,[Link],[Link]);
}
Here structure named ‘student’ is defined and then the variable of that structure st has
been declared. This structure variable st is then passed as a parameter to the print_details
() function.
Structure Pointer:
A structure pointer is a pointer variable that stores the address of a
structure.
Accessing Member using Structure Pointers:
There are two ways to access the members of the structure with the help of a
structure pointer:
1. Differencing and Using (.) Dot Operator.
2. Using ( -> ) Arrow operator.
[Link] and Using (.) Dot Operator
First method is to first dereference the structure pointer
to get to the structure and then use the dot operator to
access the member. Below is the program to access the
structure members using the structure pointer with the
help of the dot operator.
[Link] ( -> ) Arrow Operator
C language provides an array operator (->) that can be used to
directly access the structure member without using two separate
operators. Below is the program to access the structure
members using the structure pointer with the help of the
Arrow operator.
Arrays and Structures within Structures:
UNIONS
Bit-Fields:
Unlike some other computer languages, C has a built-in feature, called a
bit-field, that allows you to access a single bit. Bit-fields can be useful for
a number of reasons, such as:
• If storage is limited, you can store several Boolean (true/false)
variables in one byte.
• Certain devices transmit status information encoded into one or
more bits within a byte.
• Certain encryption routines need to access the bits within a byte
• A bit-field must be a member of a structure or union. It defines how
long, in bits, the field is to be. The general form of a bit-field
definition is.
ENUMERATION:
An enumerated data type, declared using the keyword enum, is a user-defined
data type that consists of a set of named integer constants. It improves program
readability and helps prevent invalid values.
Declaration of Enumerated Data Type:
enum enum_name
{
constant1,
constant2,
...
By};default:
The first constant has value 0
Each subsequent constant increases by 1
Example: Enum Declaration
enum Day {
MON, Here:
TUE, MON = 0, TUE = 1, ..., SUN = 6
WED,
THU,
FRI,
SAT,
SUN
};
Using Size of to Ensure Portability:
Typedef:
The typedef keyword in C is used to create an alias (an alternative
name) for an existing data type, not a new type itself. Its primary
purpose is to improve code readability, simplify complex declarations, and
enhance code portability.
General Form: