Introduction:
- Applications are used to store and process the information
- We store information using variables and process using functions.
- We use different types of variables to store the information as follows.
Primitive variable:
- Primitive Variable stores only one value at a time.
- int a = 10;
Array variable:
- Array stores more than one value but of same type.
- int arr[5] = {10, 20, 30, 40, 50};
Structure variable: stores more than one value of different data types. We define structures using struct keyword
struct Employee { int id; char name[20]; float salary; }; struct Employee e = {101, “Amar”, 35000}; |
Array of Structures: used to store more than one record details.
struct Employee{ int id; char name[20]; float salary; }; struct Employee arr[3]; |
Program to find the size (memory allocated to structure): sizeof() functions returns the total number bytes allocated to structure.

Program to display Size of Employee Structure:
#include<stdio.h> struct Emp { int id; char name[20]; float salary; }; int main() { struct Emp x; printf(“size of Emp : %d \n”, sizeof(x)); printf(“size of Emp : %d \n”, sizeof(struct Emp)); return 0; } |
Structure initialization:
- Like Primitive types and Arrays, we can assign values directly to structure variables at the time of declaration.
- struct identity var = {val1, val2, val3 …};
- Accessor (dot operator): We must access the locations of structure through dot(.) operator
#include<stdio.h> struct Employee { int id; char name[20]; float salary; }; int main() { struct Employee e = {101, “amar”, 35000}; printf(“Employee id : %d \n”, e.id); printf(“Employee name : %s \n”, e.name); printf(“Employee salary : %f \n”, e.salary); return 0; } |

Program to pass structure as parameter to function:
- Functions can take structure as input parameter.
- We pass the structure variable as parameter and collect into same type of structure variable.

Passing Structure as an input to Parameter:
#include<stdio.h> struct Employee { int id; char name[20]; float salary; }; void display(struct Employee); int main() { struct Employee x = {101, “Amar”, 35000}; display(x); return 0; } void display(struct Employee y) { printf(“Emp id : %d \n”, y.id); printf(“Emp name : %s \n”, y.name); printf(“Emp salary : %f \n”, y.salary); } |
Program to define a function that returns structure:
#include<stdio.h> struct Emp { int id; char name[20]; float salary; }; struct Emp collect(); int main() { struct Emp y; y = collect(); printf(“Emp id : %d \n”, y.id); printf(“Emp name : %s \n”, y.name); printf(“Emp salary : %f \n”, y.salary); return 0; } struct Emp collect() { struct Emp x = {101, “Amar”, 35000}; return x; } |
Array of Structures: by creating array type variable to structure data type, we can store multiple records like Employee details, Customer details, Student details, Account details etc.
struct Emp { …. }; | struct Emp e; -> store 1 record struct Emp e1, e2, e3; -> store 3 records struct Emp arr[100]; -> store 100 records |
Program to store multiple records into Array variable of structure type:
#include<stdio.h> struct Emp { int id; char name[20]; float salary; }; int main() { struct Emp arr[3]; int i; printf(“Enter 3 Emp records : \n”); for(i=0 ; i<3 ; i++) { printf(“Enter Emp-%d details : \n”, i+1); scanf(“%d%s%f”, &arr[i].id, arr[i].name, &arr[i].salary); } printf(“Employee details are : \n”); for(i=0 ; i<3 ; i++) { printf(“%d \t %s \t %f \n”, arr[i].id, arr[i].name, arr[i].salary); } return 0; } |

Arrays in structure: If the structure contains Array type variable, we need to process the data using loops.

#include<stdio.h> struct Student { int id; char name[20]; int marks[5]; }; int main() { struct Student s; int i; printf(“Enter Student id and name : \n”); scanf(“%d%s”, &s.id, s.name); printf(“Enter student marks of 5 subjects : \n”); for(i=0 ; i<5 ; i++) { scanf(“%d”, &s.marks[i]); } printf(“Student details are : \n”); printf(“%d, %s, “, s.id, s.name); for(i=0 ; i<5 ; i++) { printf(“%d ,”, s.marks[i]); } return 0; } |