2
Most read
5
Most read
21
Most read
TITLE: CALENDAR
PROGRAM USING DYNAMIC
MEMORY ALLOCATION IN C
•Subtitle: With malloc() and
typedef struct
Objective
•To implement a weekly calendar using:
•Dynamic memory allocation
•Structures with typedef
•Functions: create(), read(), display()
KEY CONCEPTS
• Dynamic Memory Allocation
• Structures in C
• Pointer handling with malloc and free
• Function modularity
Dynamic Memory Allocation
•Definition:
•Allocates memory during runtime
•Functions Used:
•malloc(), calloc(), free()
•Syntax:
ptr = (castType*) malloc(size);
Structure Declaration
typedef
struct { char *name;
int date;
char *description; } Day;
•name: Pointer to dynamically allocated string
•date: Integer
•description: Pointer to dynamically allocated string
Function: create()
•Allocates memory for 7 Day structs:
Day *calendar = (Day *)malloc(sizeof(Day) * 7);
•Checks for NULL pointer
•Returns pointer to calendar
Function: read()
•Reads day name, date, and activity description
•Dynamically allocates memory for each string
calendar[i].name = malloc(strlen(temp) + 1); strcpy(calendar[i].name, temp);
FUNCTION: DISPLAY()
•Iterates over array of Day and prints:
printf("%s %d: %sn", name, date, description);
FUNCTION: FREEING MEMORY
for (int i = 0; i < 7; i++)
{
free(calendar[i].name);
free(calendar[i].description);
} free(calendar);
•Avoids memory leaks
PROGRAM OUTPUT
•Simulated input/output:
Monday 10: Work from home
Tuesday 11: Meet with client ...
Sunday 16: Go out with friends
VIVA QUESTIONS - PART 1
Q1: What is dynamic allocation in C?
Q2: Define malloc() with syntax
Q3: Difference between malloc() and calloc()
VIVA QUESTIONS - PART 2
Q4: Purpose of free()
Q5: Use of typedef struct
Example:
typedef struct person { char name[20]; int age; } Person;
CONCLUSION
• Demonstrated:
• Use of dynamic memory
• Struct and typedef
• Modular programming
• Improved memory efficiency
• Practice with input/output and pointers
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
{
char *name;
int date;
char *description;} Day;
// FUNCTION TO CREATE A CALENDARDAY
*create()
{
Day *calendar = (Day *)malloc(sizeof(Day) * 7);
if (calendar = = NULL)
{
printf("Error allocating memory for calendarn");
exit(1);
} return calendar;
}
// FUNCTION TO READ DATA FROM THE
KEYBOARD
void read(Day *calendar)
{
for (int i = 0; i < 7; i++)
{
printf("Enter the name of day %d: ", i + 1);
char *name = (char *)malloc(sizeof(char) * 100);
if (name == NULL)
{ printf("Error allocating memory for day namen");
exit(1);
}
scanf("%s", name);
calendar[i].name = name;
printf("Enter the date of day %d: ", i + 1);
int date;
scanf("%d", &date);
calendar[i].date = date;
printf("Enter the description of activity for day %d: ", i + 1);
char *description = (char *)malloc(sizeof(char) * 100);
if (description == NULL)
{
printf("Error allocating memory for activity descriptionn");
exit(1);
}
scanf("%s", description);
calendar[i].description = description;
}
}
// Function to print weeks activity details report on
screen
Void display(Day *calendar)
{ printf("nWeek Activity Details Reportn");
printf("----------------------------n");
for (int i = 0; i < 7; i++)
{
printf("%s %d: %sn", calendar[i].name, calendar[i].date,
calendar[i].description);
}
}
int main( )
{
// Create a calendar
Day *calendar = create();
// Read data from the keyboard
read(calendar);
// Display weeks activity details report on screen
display(calendar);
// Free the memory allocated for the
calendar
for (int i = 0; i < 7; i++)
{ free(calendar[i].name);
free(calendar[i].description);
} free(calendar); return 0
;}

More Related Content

PPTX
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
PPT
DATA STRUCTURE AND APPLICATIONS -MODULE1.PPT
PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
PDF
Storytelling For The Web: Integrate Storytelling in your Design Process
PDF
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
PDF
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
PDF
2024 State of Marketing Report – by Hubspot
PDF
Everything You Need To Know About ChatGPT
ANALYSIS AND DESIGN OF ALGORITHMS -M1-PPT
DATA STRUCTURE AND APPLICATIONS -MODULE1.PPT
2024 Trend Updates: What Really Works In SEO & Content Marketing
Storytelling For The Web: Integrate Storytelling in your Design Process
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
2024 State of Marketing Report – by Hubspot
Everything You Need To Know About ChatGPT

Recently uploaded (20)

PDF
Cryptography and Network Security-Module-I.pdf
PDF
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
PDF
Beginners-Guide-to-Artificial-Intelligence.pdf
PDF
Research on ultrasonic sensor for TTU.pdf
PDF
Mechanics of materials week 2 rajeshwari
PPT
Programmable Logic Controller PLC and Industrial Automation
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PPT
UNIT-I Machine Learning Essentials for 2nd years
PPTX
Wireless sensor networks (WSN) SRM unit 2
PDF
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
PPTX
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx
PDF
electrical machines course file-anna university
PPTX
Micro1New.ppt.pptx the mai themes of micfrobiology
PDF
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
PPTX
Solar energy pdf of gitam songa hemant k
PPTX
Software-Development-Life-Cycle-SDLC.pptx
PPTX
Micro1New.ppt.pptx the main themes if micro
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PPTX
Design ,Art Across Digital Realities and eXtended Reality
PPTX
AI-Reporting for Emerging Technologies(BS Computer Engineering)
Cryptography and Network Security-Module-I.pdf
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
Beginners-Guide-to-Artificial-Intelligence.pdf
Research on ultrasonic sensor for TTU.pdf
Mechanics of materials week 2 rajeshwari
Programmable Logic Controller PLC and Industrial Automation
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
UNIT-I Machine Learning Essentials for 2nd years
Wireless sensor networks (WSN) SRM unit 2
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
WN UNIT-II CH4_MKaruna_BapatlaEngineeringCollege.pptx
electrical machines course file-anna university
Micro1New.ppt.pptx the mai themes of micfrobiology
[jvmmeetup] next-gen integration with apache camel and quarkus.pdf
Solar energy pdf of gitam songa hemant k
Software-Development-Life-Cycle-SDLC.pptx
Micro1New.ppt.pptx the main themes if micro
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Design ,Art Across Digital Realities and eXtended Reality
AI-Reporting for Emerging Technologies(BS Computer Engineering)
Ad
Ad

DATA STRCUTURE LABORATORY -BCSL305(PRG1)

  • 1. TITLE: CALENDAR PROGRAM USING DYNAMIC MEMORY ALLOCATION IN C
  • 2. •Subtitle: With malloc() and typedef struct
  • 3. Objective •To implement a weekly calendar using: •Dynamic memory allocation •Structures with typedef •Functions: create(), read(), display()
  • 4. KEY CONCEPTS • Dynamic Memory Allocation • Structures in C • Pointer handling with malloc and free • Function modularity
  • 5. Dynamic Memory Allocation •Definition: •Allocates memory during runtime •Functions Used: •malloc(), calloc(), free() •Syntax: ptr = (castType*) malloc(size);
  • 6. Structure Declaration typedef struct { char *name; int date; char *description; } Day; •name: Pointer to dynamically allocated string •date: Integer •description: Pointer to dynamically allocated string
  • 7. Function: create() •Allocates memory for 7 Day structs: Day *calendar = (Day *)malloc(sizeof(Day) * 7); •Checks for NULL pointer •Returns pointer to calendar
  • 8. Function: read() •Reads day name, date, and activity description •Dynamically allocates memory for each string calendar[i].name = malloc(strlen(temp) + 1); strcpy(calendar[i].name, temp);
  • 9. FUNCTION: DISPLAY() •Iterates over array of Day and prints: printf("%s %d: %sn", name, date, description);
  • 10. FUNCTION: FREEING MEMORY for (int i = 0; i < 7; i++) { free(calendar[i].name); free(calendar[i].description); } free(calendar); •Avoids memory leaks
  • 11. PROGRAM OUTPUT •Simulated input/output: Monday 10: Work from home Tuesday 11: Meet with client ... Sunday 16: Go out with friends
  • 12. VIVA QUESTIONS - PART 1 Q1: What is dynamic allocation in C? Q2: Define malloc() with syntax Q3: Difference between malloc() and calloc()
  • 13. VIVA QUESTIONS - PART 2 Q4: Purpose of free() Q5: Use of typedef struct Example: typedef struct person { char name[20]; int age; } Person;
  • 14. CONCLUSION • Demonstrated: • Use of dynamic memory • Struct and typedef • Modular programming • Improved memory efficiency • Practice with input/output and pointers
  • 15. PROGRAM: #include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct { char *name; int date; char *description;} Day;
  • 16. // FUNCTION TO CREATE A CALENDARDAY *create() { Day *calendar = (Day *)malloc(sizeof(Day) * 7); if (calendar = = NULL) { printf("Error allocating memory for calendarn"); exit(1); } return calendar; }
  • 17. // FUNCTION TO READ DATA FROM THE KEYBOARD void read(Day *calendar) { for (int i = 0; i < 7; i++) { printf("Enter the name of day %d: ", i + 1); char *name = (char *)malloc(sizeof(char) * 100); if (name == NULL) { printf("Error allocating memory for day namen"); exit(1); }
  • 18. scanf("%s", name); calendar[i].name = name; printf("Enter the date of day %d: ", i + 1); int date; scanf("%d", &date); calendar[i].date = date; printf("Enter the description of activity for day %d: ", i + 1); char *description = (char *)malloc(sizeof(char) * 100);
  • 19. if (description == NULL) { printf("Error allocating memory for activity descriptionn"); exit(1); } scanf("%s", description); calendar[i].description = description; } }
  • 20. // Function to print weeks activity details report on screen Void display(Day *calendar) { printf("nWeek Activity Details Reportn"); printf("----------------------------n"); for (int i = 0; i < 7; i++) { printf("%s %d: %sn", calendar[i].name, calendar[i].date, calendar[i].description); } }
  • 21. int main( ) { // Create a calendar Day *calendar = create(); // Read data from the keyboard read(calendar);
  • 22. // Display weeks activity details report on screen display(calendar); // Free the memory allocated for the calendar for (int i = 0; i < 7; i++) { free(calendar[i].name); free(calendar[i].description); } free(calendar); return 0 ;}