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);
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
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);
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
;}