CSL 102-
Data Structures
Module 2
Computer Science and Engineering
Indian Institute of Information Technology, Nagpur.
1
24-02-2026
Dynamic Memory Allocation
Dynamic Memory Allocation
malloc()
malloc()
malloc()
calloc()
calloc()
calloc()
realloc()
realloc()
realloc()
#include<stdio.h> //Memory Realoction
#include<stdlib.h> ptr = (int*)realloc(ptr, 4*sizeof(int));
if(ptr==NULL)
{
int main() printf("Memory not available");
{ exit(1);
int i; }
int *ptr = (int*) malloc(2*sizeof(int)); printf("Enter 2 more numbers\n");
for(i=2; i<4; i++)
scanf("%d", ptr+i);
if(ptr==NULL)
{
printf("Memory not available"); // printing
exit(1); printf("Numbers are \n");
} for(i=0; i<4; i++)
printf("Enter 2 numbers\n"); printf("%d\t", *(ptr+i));
for(i=0; i<2; i++){ return 0;
scanf("%d", ptr+i); }
}
free()
malloc()
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.
• The items in the structure are called its member and they can be of any valid
data type.
• Structures are used to represent a record. Suppose you want to keep track of
your books in a library. You might want to track the following attributes about
each book.
Title
Author
Subject
Book ID
Structures
struct structure_name
{ struct employee
data_type member1; { int id;
data_type member2; char name[20];
. float salary;
. };
data_type memeberN;
};
Declaring structure variable
• We can declare a variable for the structure so that we can access the
member of the structure easily. There are two ways to declare structure
variable:
1. By struct keyword within main() function
2. By declaring a variable at the time of defining the structure.
Declaring structure variable
struct employee struct employee
{ int id; { int id;
char name[50]; char name[50];
float salary; float salary;
}; }e1,e2;
Method 1
Inside Main Method 2
struct employee e1, e2;
• The variables e1 and e2 can be used to access the values stored in the structure.
• Here, e1 and e2 can be treated in the same way as the objects in C++ and Java.
Accessing members of the structure
• There are two ways to access structure members:
1. By . (member or dot operator)
2. By -> (structure pointer operator)
Accessing members of the structure
#include<stdio.h>
#include <string.h>
struct employee
{ int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
int main( )
{
//store first employee information Output:
[Link]=101;
strcpy([Link], "Sonu Jaiswal");//copying string into char array employee 1 id : 101
[Link]=56000; employee 1 name : Sonoo Jaiswal
//store second employee information employee 1 salary : 56000.000000
[Link]=102;
employee 2 id : 102
strcpy([Link], "James Bond");
[Link]=126000; employee 2 name : James Bond
//printing first employee information employee 2 salary : 126000.000000
printf( "employee 1 id : %d\n", [Link]);
printf( "employee 1 name : %s\n", [Link]);
printf( "employee 1 salary : %f\n", [Link]);
//printing second employee information
printf( "employee 2 id : %d\n", [Link]);
printf( "employee 2 name : %s\n", [Link]);
printf( "employee 2 salary : %f\n", [Link]);
return 0;
}
typedef
#include <stdio.h>
typedef struct student
{
char name[20];
int age;
}stud;
int main() Enter the details of student s1:
{
Enter the name of the student: Peter
stud s1;
printf("Enter the details of student s1: "); Enter the age of student: 28
printf("\nEnter the name of the student:"); Name of the student is : Peter
scanf("%s",&[Link]); Age of the student is : 28
printf("\nEnter the age of student:");
scanf("%d",&[Link]);
printf("\n Name of the student is : %s", [Link]);
printf("\n Age of the student is : %d", [Link]);
return 0;
}
Array of Structures
• The main advantage of an array is we can represent multiple values with a single
variable.
struct student
{
char name[50];
char class[100];
int roll_number;
float marks[5];
};
struct student s1, s2, s3;
Instead, we can use an array of structures like:
struct student s[3];
#include<stdio.h>
struct student {
char name[50];
char Class[100];
int roll_number;
float marks[5];
};
printf("\n");
int main() printf("Name\t\tRoll
{ no\t\t\tClass\t\t\t\tMarks\n");
struct student s[2]; for (int i = 0; i < 2; i++)
for (int i = 0; i < 2; i++) {
{ printf("%s\t\t%d\t\t\t%s\t\t",
printf("\nEnter details of student %d\n", i + 1); s[i].name, s[i].roll_number, s[i].Class);
printf("Enter name: "); for (int j = 0; j < 5; j++)
scanf("%s", s[i].name); {
printf("\nEnter roll no: "); printf("%.2f\t", s[i].marks[j]);
scanf("%d", &s[i].roll_number); }
printf("\nEnter class: "); printf("\n");
scanf("%s", s[i].Class); }
for (int j = 0; j < 5; j++)
{ return 0;
printf("\nEnter the marks in subject %d (out of 100): ", j + 1); }
scanf("%f", &s[i].marks[j]);
}
printf("\n");
}
OUTPUT:
Enter details of student 1
Enter name: Aaradhya
Enter roll no: 1
Enter class: A
Enter the marks in subject 1 (out of 100): 100
Enter the marks in subject 2 (out of 100): 99
Enter the marks in subject 3 (out of 100): 98
Enter the marks in subject 4 (out of 100): 97
Enter the marks in subject 5 (out of 100): 99
Enter details of student 2
Enter name: Scaler
Enter roll no: 2
Enter class: A
Enter the marks in subject 1 (out of 100): 100
Enter the marks in subject 2 (out of 100): 100
Enter the marks in subject 3 (out of 100): 99
Enter the marks in subject 4 (out of 100): 98
Enter the marks in subject 5 (out of 100): 100
Name Roll no Class Marks
Aaradhya 1 A 100.00 99.00 98.00 97.00 99.00
Scaler 2 A 100.00 100.00 99.00 98.00 100.00
Accessing members of the structure
• There are two ways to access structure members:
1. By . (member or dot operator)
2. By -> (structure pointer operator)
Structure Pointer in C
• We can define a pointer that points to the structure like any other variable.
Such pointers are generally called Structure Pointers.
• We can access the members of the structure pointed by the structure
pointer using the ( -> ) arrow operator.
Example of Structure Pointer
// C program to illustrate the structure pointer
#include <stdio.h>
// structure declaration
struct Point
{
int x, y;
}; Output:
int main()
12
{
struct Point str = { 1, 2 };
// ptr is a pointer to structure str
struct Point* ptr = &str;
// Accessing structure members using structure pointer
printf("%d %d", ptr->x, ptr->y);
return 0;
}
Problem Statement 1: Employee Database
You are tasked with creating a program to manage an employee database. The program should:
Define a structure named Employee with the following members:
name (string): to store the employee's name.
employeeId (integer): to store the employee's unique ID.
salary (float): to store the employee's salary.
Declare an array of Employee structures to store information about three employees.
Implement a loop to input information for each employee, including their name, employee ID,
and salary.
Display the information for each employee, including their name, employee ID, and salary.
Arrays versus Linked Lists
•In arrays
•Elements are stored in a contiguous memory Index Data Address
locations 0 15 100
•Arrays are static data structure unless we use 1 20 104
dynamic memory allocation 2 25 108
• Arrays are suitable for 30 112
3
▪ Inserting/deleting an element at the end.
▪ Randomly accessing any element.
29