Structures in C
User-defined composite types for grouping heterogeneous variables
What is a Structure?
A Structure is a user-defined composite data type in C that enables storing multiple related variables of different data types (heterogeneous data) under a single unique name.
The struct keyword is used to declare a structure. The individual variables declared within a structure are known as 'members of the structure.'
Instances of Structure
Structure variables (instances) can be declared in two distinct ways:
struct emp_info {
char emp_id[10];
char name[100];
float sal;
} emp; // Variable created immediatelystruct emp_info {
char emp_id[10];
char name[100];
float sal;
};
struct emp_info emp; // Variable created using tagAccessing Structure Members
Structure members are accessed using the member operator '.' (known as the dot operator or period operator): Structure_var.member
#include <stdio.h>
struct comp_info {
char name[50];
char addr[100];
} info;
void main() {
printf("Enter company Name: ");
scanf("%s", info.name);
printf("Enter Address: ");
scanf("%s", info.addr);
printf("\n\nCompany Name: %s", info.name);
printf("\nAddress: %s\n", info.addr);
}Enter company Name: TechnoExam Enter Address: Sangli Company Name: TechnoExam Address: Sangli
Array within a Structure
A structure member can itself be an array to hold multiple elements:
#include <stdio.h>
struct result {
int rno;
int marks[3];
} res;
void main() {
int i, total = 0;
printf("Enter Roll Number: ");
scanf("%d", &res.rno);
printf("Enter 3 subject marks: ");
for (i = 0; i < 3; i++) {
scanf("%d", &res.marks[i]);
total += res.marks[i];
}
printf("\nRoll Number: %d", res.rno);
printf("\nMarks are: ");
for (i = 0; i < 3; i++) {
printf("%d ", res.marks[i]);
}
printf("\nTotal is: %d\n", total);
}Enter Roll Number: 1 Enter 3 subject marks: 63 66 68 Roll Number: 1 Marks are: 63 66 68 Total is: 197
Array of Structures
An array of structures represents multiple database-style records with identical schemas:
#include <stdio.h>
struct emp_info {
int emp_id;
char name[50];
} emp[2];
void main() {
int i;
for (i = 0; i < 2; i++) {
printf("Enter Employee ID: ");
scanf("%d", &emp[i].emp_id);
printf("Enter Employee Name: ");
scanf("%s", emp[i].name);
}
printf("\n--- Employee Directory ---\n");
for (i = 0; i < 2; i++) {
printf("ID: %d | Name: %s\n", emp[i].emp_id, emp[i].name);
}
}Enter Employee ID: 1 Enter Employee Name: ABC Enter Employee ID: 2 Enter Employee Name: XYZ --- Employee Directory --- ID: 1 | Name: ABC ID: 2 | Name: XYZ
Nested Structures (Structures within Structures)
A structure can be embedded within another structure, forming hierarchical data models accessed via chained dot notation (e.g., outer.inner.field):
#include <stdio.h>
struct stud_res {
int rno;
char std[20];
struct stud_marks {
char subj_name[30];
int subj_mark;
} marks;
} result;
void main() {
result.rno = 1;
result.marks.subj_mark = 85;
printf("Student Roll No: %d\n", result.rno);
printf("Subject Marks: %d\n", result.marks.subj_mark);
}Student Roll No: 1 Subject Marks: 85