I have a while loop, where in each itteration of the while loop I malloc a struct object. Then at the end of the loop I add it to the end of a linked list. Now my concern is, I have the same variable name when I malloc each time in the loop, so I dont understand how this works after I attach the lastest malloced object to the linked list, what happens to the others that I malloced that had the same name.
My code:
struct Student{
char *name;
int IDno;
}Student;
someFunction(){
x = 0;
while(x!=6){
Student *name = malloc(sizeof(Student)); <----- Confusion here
addToLinkedList(LinkedListStruct,name);
}
Is it alright that I have name
being malloced each time in the loop. Can someone explain to me what happens if I malloc in this way, add it to the end of a linked list and then go into the next loop and do this same.