Structure Programs
Structure Programs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a structure to represent a day's
activity
struct DayActivity {
char *dayName;
int date;
char *activityDescription;
};
// Function to create a day's activity
struct DayActivity createDayActivity(const
char* dayName, int date, const char*
activityDescription) {
struct DayActivity day;
day.dayName = strdup(dayName);
day.date = date;
day.activityDescription =
strdup(activityDescription);
return day;
}
// Function to read a day's activity
void readDayActivity(const struct DayActivity
*day) {
printf("Day: %s\n", day->dayName);
printf("Date: %d\n", day->date);
printf("Activity: %s\n", day-
>activityDescription);
}
// Function to free the memory allocated for a
day's activity
void freeDayActivity(struct DayActivity *day)
{
free(day->dayName);
free(day->activityDescription);
}
int main() {
// Create an array of 7 DayActivity structures
struct DayActivity calendar[7];
// Initialize each element of the calendar
array
calendar[0] = createDayActivity("Monday",
1, "Work");
calendar[1] = createDayActivity("Tuesday",
2, "Gym");
calendar[2] =
createDayActivity("Wednesday", 3, "Meeting");
calendar[3] = createDayActivity("Thursday",
4, "Shopping");
calendar[4] = createDayActivity("Friday", 5,
"Lunch with friends");
calendar[5] = createDayActivity("Saturday",
6, "Hiking");
calendar[6] = createDayActivity("Sunday", 7,
"Relax");
// Read and display the day activities
for (int i = 0; i < 7; i++) {
readDayActivity(&calendar[i]);
}
// Free the allocated memory
for (int i = 0; i < 7; i++) {
freeDayActivity(&calendar[i]);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DAYS_IN_WEEK 7
// Define the structure for a calendar day
struct CalendarDay {
char *dayName;
int date;
char *activityDescription;
};
int main() {
// Create an array of CalendarDay structures
struct CalendarDay
week[DAYS_IN_WEEK];
// Initialize the calendar with data for each
day
for (int i = 0; i < DAYS_IN_WEEK; i++) {
week[i].dayName = (char *)malloc(20 *
sizeof(char)); // Allocating memory for
dayName
week[i].activityDescription = (char
*)malloc(100 * sizeof(char)); // Allocating
memory for activityDescription
// Example data initialization
switch (i) {
case 0:
strcpy(week[i].dayName, "Monday");
week[i].date = 1;
strcpy(week[i].activityDescription,
"Work");
break;
case 1:
strcpy(week[i].dayName, "Tuesday");
week[i].date = 2;
strcpy(week[i].activityDescription,
"Gym");
break;
case 2:
strcpy(week[i].dayName,
"Wednesday");
week[i].date = 3;
strcpy(week[i].activityDescription,
"Meeting");
break;
case 3:
strcpy(week[i].dayName,
"Thrusday");
week[i].date = 4;
strcpy(week[i].activityDescription,
"Shopping");
break;
case 4:
strcpy(week[i].dayName, "Friday");
week[i].date = 5;
strcpy(week[i].activityDescription,
"Lunch with friends");
break;
case 5:
strcpy(week[i].dayName, "Saturday");
week[i].date = 5;
strcpy(week[i].activityDescription,
"Hiking");
break;
case 6:
strcpy(week[i].dayName, "Sunday");
week[i].date = 5;
strcpy(week[i].activityDescription,
"Relax");
break;
default:
break;
}
}
// Access and print the calendar
for (int i = 0; i < DAYS_IN_WEEK; i++) {
printf("%s (Date %d): %s\n",
week[i].dayName, week[i].date,
week[i].activityDescription);
}
// Free allocated memory
for (int i = 0; i < DAYS_IN_WEEK; i++) {
free(week[i].dayName);
free(week[i].activityDescription);
}
return 0;
}
Structure Union
1. The keyword
1. The keyword struct is
union is used to
used to define structure.
define union.
2. It allocates piece
2. The compiler allocates
of memory that is
the memory for each
big enough to hold
member of the structure
the largest variable
variable.
of type in union.
3. Memory
3. Separate memory is allocated is shared
allocated for each member by individual
of the structure variable. members of the
union variable.
4. Changing the value of 4. Changing the
any of the structure value of any of the
member will not affect union member will
other members of modify other
the structure. member values.
5. At any time,
5. At any time,
programmer can
programmer can access all
access only one
the members of structure
member of union
variable.
variable.
6. Only the first or
6. All members of
any single member
structure variable can be
of union variable
initialized.
can be initialized.
Table: Differences between structure and
union