简体   繁体   English

结构 function 指针参数?

[英]Structure function pointer parameter?

Fields of Student: name, lastName, studentId, mid1Grade, mid2Grade, finalGrade, average学生字段:name、lastName、studentId、mid1Grade、mid2Grade、finalGrade、average

Fields of Course: courseName, courseCode, myStudentArray (array of Student structures),currentStudentCount课程字段:courseName、courseCode、myStudentArray(学生结构数组)、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.好的,所以我写了第一个 function 但有一个我不明白的错误。 First of all the first function and what it does:首先是第一个 function 及其作用:

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. createNewStudent:提示用户输入新学生的姓名、姓氏和id。用户输入的值被分配给位于myCourse指向的课程变量myStudentArray中的学生的字段。 currentStudentCount will be updated so that it designates the slot allocated for the student inserted next. currentStudentCount 将被更新,以便它指定为下一个插入的学生分配的插槽。

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(input == 1) 调用 function 时,它会给出错误:“struct”之前的预期表达式,但我不明白这一点,因为 *myCourse 只是指向 Course 结构的指针,不是吗???? If I can understand this ı will be able to the the next functions I think如果我能理解这一点,我将能够实现我认为的下一个功能

Is the function correct?? function 是否正确? I dont know why this doesnt work我不知道为什么这不起作用

Ok I tried to use the struct Student myStudentArray[5];好的,我尝试使用struct Student myStudentArray[5]; to get name, lastname,id like so (structs are the same)像这样获取姓名,姓氏,id(结构相同)

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可能在此使用未初始化may be used uninitialized in this function [-Wmaybe-uninitialized]和分段错误

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 .如果要创建新学生,则应使用 student struct 作为此 function 的参数,而不是使用struct Course

void createNewStudent(struct Student *s1)

Then, this function becomes as:然后,这个 function 变为:

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 .如果要测试,在主 function 中,可以声明值stud或指向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: 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.对于Course structure ,您可以创建一个 function 作为 function createNewStudent但对于struct Course来创建新课程。 After creating new 5 students (for example the code above), you can copy the myStudentArray to new_course.myStudentArray .创建新的 5 个学生后(例如上面的代码),您可以将myStudentArray复制到new_course.myStudentArray Then now you have the info of 5 students in new_course .那么现在你在new_course中有 5 个学生的信息。 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.当您将值从一个数组复制到另一个时,您可以使用memcpy或使用一个循环将每个元素从一个数组复制到另一个数组。 Do not use something like myStudentArray = new_course.myStudentArray for the array.不要将myStudentArray = new_course.myStudentArray用于数组。

You are making a declaration as a parameter of the createNewStudent() function.您正在将声明作为createNewStudent() function 的参数。 In C, functions require expressions as parameters, which is why you got the error message "expected expression...".在 C 中,函数需要表达式作为参数,这就是您收到错误消息“expected expression...”的原因。

So, just create the struct pointer before you call the function:因此,只需在调用 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.注意malloc()的使用,它返回一个指向 memory 中一个位置的指针,该位置足够大以容纳该特定的Course结构。 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.在处理指向结构的指针时,您需要为最终将指向的结构分配 memory,以避免取消引用 memory 的未分配区域。

In your function CerateNewStudent , the proper way to address the variables into which to place the data read by scanf should be:在您的 function CerateNewStudent中,处理放置scanf读取的数据的变量的正确方法应该是:

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

as the variable to read name into.作为要读取name的变量。 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.首先应该创建一门课程,然后可以将学生添加到其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM