I'm trying to write a program for students linked list in C. Here is the struct:
typedef struct student *studp;
typedef struct student{
int number;
char *name;
char *date;
unsigned grade;
studp next;
}stud;
Now I want to write a function which creates new node and assign values to members. I'm not sure how to do it, here is the function:
studp create_stud(int number, char *name, char *date, unsigned grade){
studp item = (studp)malloc(sizeof(stud));
if(!item){
printf("Cannot allocate memory\n");
exit(1);
}
item->number = number;
item->name = .....
return item;
} how should I assign the text to the member name and date? can I use the operator '=':
item->name = name;
or I should allocate the memory with malloc() ? and if I use malloc() should I free the pointer in the and of the program or I just can release the node? Thanks!
studpthat looks confusingly likestrdup()was going to make me crazy. - Iharob Al Asimitypedef POINTER_TYPE *NON_POINTER_TYPEbecause it's confusing. - Iharob Al Asimi