Consider the following program
typedef struct
{
char *name;
int id;
}Student;
int main()
{
Student std1,std2; //case 1
std1.name =(char*) malloc(7);
strcpy(std1.name, "Daniel");
puts(std1.name);
char name[7]={'D','a','n','i','e','l','\0'}; //case 2
std2.name = name;
puts(std2.name);
}
Case 1 place the name of the student in the heap memory while case 2 place the name of student2 in the sack memory, if I understand correctly.
My question is, why would I want to place the name (or any other field of structure) in the heap memory?