I am trying to a create a linked list. When I add to the list in the same function that I create the object in, it works.
Definitions:
typedef struct student {
int num;
char* name;
} student;
typedef struct container {
student* data;
struct container* next;
} container ;
All the objects I use are initialized like this:
student stu1;
stu1.num = 6;
stu1.name = "grefagf";
front = createContainer(&stu1);
back = front;
student stu2;
stu2.num = 3;
stu2.name = "dsghjyreawre";
student stu3;
stu3.num = 4;
stu3.name = "dsghhjrant";
student stu4;
stu4.num = 213;
stu4.name = "fdsafgrw";
When I add these elements to a list in the main function like this:
container* tmp;
tmp = createContainer(&stu2);
back->next = tmp;
back = tmp;
tmp = createContainer(&stu3);
back->next = tmp;
back = tmp;
tmp = createContainer(&stu4);
back->next = tmp;
back = tmp;
it works properly, outputting this:
1: 6 grefagf
2: 3 dsghjyreawre
3: 4 dsghhjrant
4: 213 fdsafgrw
when I print it using another function I made.
But if I create a function called add() and pass stu2, stu3..., like so:
int add(student to_add) {
container* tmp;
tmp = createContainer(&to_add);
printf("added: (%d, %s)\n", tmp->data->num, tmp->data->name);
back->next = tmp;
back = tmp;
return 1;
}
then do this in the main function:
add(stu2);
add(stu3);
add(stu4);
it outputs this:
1: 6 grefagf
2: 41096808 fdsafgrw
3: 41096808 fdsafgrw
4: 41096808 fdsafgrw
Heres the source in case you need it:
Non-function example: https://pastebin.com/ZLqTzp4t
#include <stdio.h>
#include <stdlib.h>
typedef struct student {
int num;
char* name;
} student;
typedef struct container {
student* data;
struct container* next;
} container ;
container* back;
container* front;
container* createContainer(student* data) {
container* tmp = malloc(sizeof(container));
tmp->data = data;
tmp->next = NULL;
return tmp;
}
void printList(container* front) {
container* tmp = front;
int i;
i=0;
while (tmp != NULL) {
i++;
printf("%d:\t%d\t\t%s\n", i, tmp->data->num, tmp->data->name);
tmp = tmp->next;
}
}
int main(void) {
student stu1;
stu1.num = 6;
stu1.name = "grefagf";
front = createContainer(&stu1);
back = front;
student stu2;
stu2.num = 3;
stu2.name = "dsghjyreawre";
student stu3;
stu3.num = 4;
stu3.name = "dsghhjrant";
student stu4;
stu4.num = 213;
stu4.name = "fdsafgrw";
container* tmp;
tmp = createContainer(&stu2);
back->next = tmp;
back = tmp;
tmp = createContainer(&stu3);
back->next = tmp;
back = tmp;
tmp = createContainer(&stu4);
back->next = tmp;
back = tmp;
printf("front\n");
printList(front);
printf("\ntop\n");
printList(back);
return EXIT_SUCCESS;
}
Function example: https://pastebin.com/TyQY4j5k
#include <stdio.h>
#include <stdlib.h>
typedef struct student {
int num;
char* name;
} student;
typedef struct container {
student* data;
struct container* next;
} container ;
container* back;
container* front;
container* createContainer(student* data) {
container* tmp = malloc(sizeof(container));
tmp->data = data;
tmp->next = NULL;
return tmp;
}
int add(student to_add) {
container* tmp;
tmp = createContainer(&to_add);
printf("added: (%d, %s)\n", tmp->data->num, tmp->data->name);
back->next = tmp;
back = tmp;
return 1;
}
void printList(container* front) {
container* tmp = front;
int i;
i=0;
while (tmp != NULL) {
i++;
printf("%d:\t%d\t\t%s\n", i, tmp->data->num, tmp->data->name);
tmp = tmp->next;
}
}
int main(void) {
student stu1;
stu1.num = 6;
stu1.name = "grefagf";
front = createContainer(&stu1);
back = front;
student stu2;
stu2.num = 3;
stu2.name = "dsghjyreawre";
student stu3;
stu3.num = 4;
stu3.name = "dsghhjrant";
student stu4;
stu4.num = 213;
stu4.name = "fdsafgrw";
add(stu2);
add(stu3);
add(stu4);
printf("front\n");
printList(front);
printf("\ntop\n");
printList(back);
return EXIT_SUCCESS;
}
add(), but you've not included the definition ofstudent.student stu1; stu1.num = 6; stu1.name = "grefagf";is not an initializer forstu1; it assigns values to it. It would be initialized withstudent stu1 = { 6, "grefagf" };orstudent stu1 = { .num = 6, .name = "grefagf" };— where the second uses designated initializers. (IOW: "initializer" is a technical term in C and applies to 'assigned when defined'.)