1 | initial version |
To allocate space for a struct using malloc, follow these steps:
typedef struct {
int id;
char name[20];
float salary;
} Employee;
Employee *emp = (Employee *)malloc(sizeof(Employee));
if (emp == NULL) {
printf("Memory allocation failed");
exit(1);
}
emp->id = 1001;
strcpy(emp->name, "John");
emp->salary = 50000.00;
free(emp);