I'm trying to create a linked list of courses with dynamic allocation. I keep getting errors. can someone tell me what is the problem?
One of the errors is:
Dereferencing NULL pointer 'coursesList'.
Should I allocate memory to each of the fields of the struct?
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct Course
{
int a;
char courseNumber[5];
char courseName[30];
struct Course* next;
};
struct Course* updateCoursesList(char courseName[], char courseNumber[]);
struct Course* updateCoursesList(char courseName[], char courseNumber[])
{
struct Course* coursesList = (struct Course*)malloc(sizeof(struct Course));
strcpy(coursesList->courseName, courseName);
strcpy(coursesList->courseNumber, courseNumber);
coursesList->next = NULL;
coursesList->a = 2;
printf("%d", coursesList->a);
printf("%s", coursesList->courseName);
printf("%s", coursesList->courseNumber);
return coursesList;
}
int main()
{
char newcourse[] = "math";
char newcoursenumber[] = "54321";
struct Course* A = updateCoursesList(newcourse, newcoursenumber);
}