1

Fields of Student:​ name, lastName, studentId, mid1Grade, mid2Grade, finalGrade, average

Fields of Course:​ courseName, courseCode, myStudentArray (array of Student structures),currentStudentCount

Functions:

void createNewStudent(struct Course *myCourse);

void setGradeOfStudent(struct Course *myCourse);

void findAndDisplayAverage(struct Course *myCourse);

struct Student * findStudentByID(int id, struct Course *myCourse);

void displayAverageOfAllStudents(struct Course *myCourse);

void displayAverageOfStudentsInInterval(struct Course *myCourse

ok so I have written the first function but there is an error which I dont understand. First of all the first function and what it does:

createNewStudent:​ Prompt the user to enter name, last name and id of the new student.Values entered by the user are assigned to the fields of the student residing in themyStudentArray ​of course variable pointed by ​myCourse​. ​currentStudentCount ​will be updated so that it designates the slot allocated for the student inserted next.

and my implementation:

#include <string.h>

#include <stdio.h>

struct Student {
  char name[50];
  char lastname[50];
  int id;
  int mid1;
  int mid2;
  int final;
  double average;
};

struct Course {
  char courseName[50];
  char courseCode[50];
  struct Student myStudentArray[5];
  int currentstudentcount;
};

void createNewStudent(struct Course * myCourse);
void setGradeOfStudent(struct Course * myCourse);
void findAndDisplayAverage(struct Course * myCourse);
struct Student * findStudentByID(int id, struct Course * myCourse);
void displayAverageOfAllStudents(struct Course * myCourse);
void displayAverageOfStudentsInInterval(struct Course * myCourse);

int main() {
  struct Student * stud;
  int input = 0;
  scanf("%d", & input);
  if (input == 1) {
    createNewStudent(struct Course * myCourse);
  }
}
return 0;

}

void createNewStudent(struct Course * myCourse) {
  struct Student s1;
  printf("Enter name: ");
  scanf("%[^\n]%*c", s1.name);
  printf("Enter Surname: ");
  scanf("%[^\n]%*c", s1.lastname);
  printf("Enter id: ");
  scanf("%d", & s1.id);
}

When you call the function with the if(input == 1) it gives error: expected expression before ‘struct’ but I dont understand this beacuse *myCourse is just a pointer to the Course struct isn't it ???? If I can understand this ı will be able to the the next functions I think

Is the function correct ?? I dont know why this doesnt work

Ok I tried to use the struct Student myStudentArray[5]; to get name, lastname,id like so (structs are the same)

void createNewStudent(struct Course *myCourse);
void setGradeOfStudent(struct Course *myCourse);


int main(){

  struct Course *myCourse;

  int input=0;
  scanf("%d",&input);

  if(input == 1){
      createNewStudent(myCourse);

  }

  return 0;

  } 

void createNewStudent(struct Course *myCourse){
  myCourse->currentstudentcount=0;

  printf("Enter name: ");
  scanf("%[^\n]%*c"  
  ,myCourse->myStudentArray[myCourse->currentstudentcount].name);                                                                                      

  printf("Enter Surname: ");
  scanf ("%[^\n]%*c",
  myCourse->myStudentArray[myCourse->currentstudentcount].name);

  myCourse->currentstudentcount++;

  }

I Keep getting

may be used uninitialized in this may be used uninitialized in this function [-Wmaybe-uninitialized] and Segmentation errors

14
  • 1
    There is no variable myCourse defined in main. Once you have it appropriately defined, you'd just call createNewStudent(myCourse);. And once that works, you'll need to revisit createNewStudent, which initializes a Student structure which is the local to the function, and lost as soon as the function returns. Commented May 6, 2020 at 15:31
  • ok but I dont want to lose it I want to add it to the hole struct Student to the struct Student myStudentArray[5]; so I can accses it later. How do I do that ? @dxiv Commented May 6, 2020 at 15:36
  • The cause of the compiler error is: if you pass a variable of a struct type (or whatever type) to a function, you omit the type. So the call createNewStudent(struct Course * myCourse); should be createNewStudent(myCourse); Commented May 6, 2020 at 15:39
  • @AliUlaşHayır Then you don't need the local variable at all. Replace s1 with myCourse->myStudentArray[k] for some k. Commented May 6, 2020 at 15:40
  • 1
    Allocate the memory for myCourse before calling createNewStudent function. Commented May 7, 2020 at 17:25

3 Answers 3

2
void createNewStudent(struct Course * myCourse)

If you want to create new student, you should use the student struct as the parameter of this function instead of using struct Course.

void createNewStudent(struct Student *s1)

Then, this function becomes as:

void createNewStudent(struct Student *s1) {

  printf("Enter name: ");
  scanf("%49s", s1->name);
  printf("Enter Surname: ");
  scanf("%49s", s1->lastname);
  printf("Enter id: ");
  scanf("%d", & s1->id);
}

If you want to test, in main function, you can declare the value stud or the pointer to struct Student.

For example:

int main() {
  struct Student stud;
  int input = 0;
  scanf("%d", & input);
  if (input == 1) {
    createNewStudent(&stud);
    printf("name: %s\n Surname: %s\n id = %d\n", stud.name, stud.lastname, stud.id);
  }

  return 0;
}

The complete program for test:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

struct Student {
  char name[50];
  char lastname[50];
  int id;
  int mid1;
  int mid2;
  int final;
  double average;
};

struct Course {
  char courseName[50];
  char courseCode[50];
  struct Student myStudentArray[5];
  int currentstudentcount;
};

void createNewStudent(struct Student *s1);

int main() {
  struct Student stud;
  int input = 0;
  scanf("%d", & input);
  if (input == 1) {
    createNewStudent(&stud);
    printf("name: %s\nSurname: %s\nid = %d\n", stud.name, stud.lastname, stud.id);
  }


  return 0;
}


void createNewStudent(struct Student *s1) {

  printf("Enter name: ");
  scanf("%49s", s1->name);
  printf("Enter Surname: ");
  scanf("%49s", s1->lastname);
  printf("Enter id: ");
  scanf("%d", & s1->id);
}

The output:

#./test 
1
Enter name: abc
Enter Surname: def
Enter id: 100
name: abc
Surname: def
id = 100

Update for your question in the comment:

If you want to store student info in an array, you can change the code to:

struct Student myStudentArray[5];
  int input = 0;
  scanf("%d", & input);
  if (input == 1) {
    for (int i = 0; i < 5; i++) {
        createNewStudent(&myStudentArray[i]);
    }

    for (int i = 0; i < 5; i++) {
        printf("name: %s\nSurname: %s\nid = %d\n", myStudentArray[i].name, myStudentArray[i].lastname, myStudentArray[i].id);
    }
  }

For Course structure you can create one function as the function createNewStudent but for struct Course to create the new course. After creating new 5 students (for example the code above), you can copy the myStudentArray to new_course.myStudentArray. Then now you have the info of 5 students in new_course. When you copy value from an array to another, you can use memcpy or using one loop to copy each element from one array to another one. Do not use something like myStudentArray = new_course.myStudentArray for the array.

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

6 Comments

"%[^\n]%*c", --> "%49s" is a good improvement to have a width limit, yet s will stop short if the name contains a space. Perhaps " %49[^\n]"?
If the name contains space, he should use fgets because, scanf read word by word. Am i wrong ?
@Hitokiri for example I created the student just like in the output but then I need to store all of the information about the student in the ` struct Student myStudentArray[5]; ` how do I do that because later I need to get the grade average of all the students
and the problem with not using Course structure as the function paramater is that I can not access the currentstudentcount
Yes that will help but I think I cant really explain because of bad english but I want the function parameter to be struct Course *myCourse and because Course has mystudentarray then use it in the func. I edited my question and what I tried could you please take a look? Your answer was helpfull btw thank you
|
1

You are making a declaration as a parameter of the createNewStudent() function. In C, functions require expressions as parameters, which is why you got the error message "expected expression...".

So, just create the struct pointer before you call the function:

if (input == 1) {
    struct Course *myCourse = malloc(sizeof(struct Course));
    createNewStudent(myCourse);
}

Notice the use of malloc(), which returns a pointer to a place in memory of sufficient size to hold that particular Course struct. When dealing with pointers to structs, you need to allocate memory for the structs that will ultimately be pointed to, in order to avoid dereferencing unallocated regions of memory.

Comments

1

In your function CerateNewStudent, the proper way to address the variables into which to place the data read by scanf should be:

myCourse->myStudentArray[myCourse->currentstudentcount].name

as the variable to read name into. Use this syntax for all data items to read. After that, increment the counter:

myCourse->currentstudentcount++;

Note: what is missing in all your functions (and in the assignment?) is a way to create a course. The students created are all added to courses. First a course should be created and then students can be added to it.

3 Comments

answer to your note :I honestly dont understand.the assignment pdf is clearly unattended .The only one who understands the whole question is the person who wrote it
ok so I just need to declare a variable like struct Course *mycourse in the main function and than send that to the other function ?
Sorry for my late reaction. Not only declare a pointer variable, but allocate memory to it and fill in the fields. Or declare a struct variable and initialize it.

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.