1

I have been trying to allocate memory to the pointer variable but it keeps giving me errors or segmentation fault. How do i properly initialize the pointer variable to the course struct?

 typedef struct {
    char courseId[7];
    char courseName[10];
    } Course;

    struct Student{
    char firstName[10];
    char lastName[10];
    int studentId;
     Course *course;
    };

int main() {
  int numStudents;
  printf("How many students do you wish to register?: ");
  scanf("%d", &numStudents);
  struct Student *student[numStudents];
  student[numStudents] = malloc(sizeof(*student[numStudents]));
  student[numStudents]->course = malloc(sizeof*(numStudents * Course));
  for (int i = 0; i < numStudents; i++) {
    printf("Enter student #%d First Name: ", i+1);
    scanf("%s", student[i]->firstName);
    printf("Enter student Last Name: ");
    scanf("%s", student[i]->lastName);
    printf("Enter Student ID: ");
    scanf("%d", &student[i]->studentId);
    printf("Enter Course ID: ");
    //student[i]->course = malloc(sizeof(*(student[i]->course)));
    scanf("%s", student[i]->course->courseId);
    printf("Enter Cousrse Name: ");
    //student[i]->course = malloc(sizeof(*(student[i]->course)));
    scanf("%s", student[i]->course->courseName);
  }

  for (int i = 0; i < numStudents; i++) {
    printf("Student Name: %s %s\n", student[i]->firstName, student[i]->lastName);
    printf("Student ID: %d\n", student[i]->studentId);
    printf("Course Code: %s\n", student[i]->course->courseId );
    printf("Course name: %s\n", student[i]->course->courseName);
    free(student[i]->course);
  }
1
  • Avoid width-less scanf("%s", student[i]->firstName); string input. Better to use char firstName[10]; .... scanf("%9s", student[i]->firstName); Commented Dec 14, 2019 at 7:36

1 Answer 1

1

You almost had it, there was a few corrections that needed to be made. Course object in Student doesn't need to be a pointer. The array of Student structures need to be dynamically allocated then free'd later on. -> is used as a dereference operator but in your case [] is already dereferencing so you would use . instead of -> and when you allocate memory for the array of structures it needs to be the size of the structure multiplied by the number of structures. malloc(sizeof(struct Student)*numStudents);

typedef struct {
    char courseId[7];
    char courseName[10];
} Course;

struct Student{
    char firstName[10];
    char lastName[10];
    int studentId;
    Course course;
};

int main() {
    int numStudents;
    printf("How many students do you wish to register?: ");
    scanf("%d", &numStudents);
    struct Student* students = malloc(sizeof(struct Student)*numStudents);
    for (int i = 0; i < numStudents; i++) {
        printf("Enter student #%d First Name: ", i+1);
        scanf("%s", students[i].firstName);
        printf("Enter student Last Name: ");
        scanf("%s", students[i].lastName);
        printf("Enter Student ID: ");
        scanf("%d", &students[i].studentId);
        printf("Enter Course ID: ");
        scanf("%s", students[i].course.courseId);
        printf("Enter Cousrse Name: ");
        scanf("%s", students[i].course.courseName);
    }

    for (int i = 0; i < numStudents; i++) {
        printf("Student Name: %s %s\n", students[i].firstName, students[i].lastName);
        printf("Student ID: %d\n", students[i].studentId);
        printf("Course Code: %s\n", students[i].course.courseId );
        printf("Course name: %s\n", students[i].course.courseName);
    }
    free(students);
    return 0;
}

I tried to make minimal changes to your code.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer did the job for me, Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.