I am getting myself familiar with the linked list and dynamic memory.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char *name; // name of student
char ID[7]; // student ID (nul terminated)
// you may add fields to this structure if needed
} Student;
typedef struct Course_st {
// add fields here
Student *Student_list;
struct Course * next;
} Course;
// kirjoita ohjelma tähän
int main()
{
Course *course_1 = (Course*)malloc(1*sizeof(Course));
Student *st_1 =malloc(sizeof(Student));
st_1->name = malloc(5*sizeof(char));
strcpy(st_1->name,"Hien");
strcpy(st_1->ID,"001");
course_1->Student_list = st_1;
course_1->next = malloc(sizeof(Course));
Student *st_2 = malloc(4*sizeof(Student));
st_2->name = malloc(4*sizeof(char));
strcpy(st_2->name,"Kim");
strcpy(st_2->ID,"002");
Course* next = (Course*) course_1->next;
next->Student_list = st_2;
next->next= NULL;
while(course_1 != NULL)
{
printf("%s %s\n", course_1->Student_list->name, course_1->Student_list->ID);
free(course_1->Student_list->name);
free(course_1->Student_list);
course_1 = (Course*)course_1->next;
}
free(next);
}
and I got back this error...
in use at exit: 16 bytes in 1 blocks
total heap usage: 7 allocs, 6 frees, 4,217 bytes allocated
16 bytes in 1 blocks are definitely lost in loss record 1 of 1
at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x1086EB: main (teht3.c:21)
LEAK SUMMARY:
definitely lost: 16 bytes in 1 blocks
...
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)