我有一个结构“课程”和一个函数:
typedef struct Course_s
{
char* name;
int grade;
} Course;
int courseGetGrade(Course const* course)
{
assert(course);
return course -> grade;
}
和另一个结构“成绩单”和一个函数:
typedef struct Transcript_s
{
char* name;
struct Course** courseArray;
} Transcript;
double tsAverageGrade(Transcript const *t)
{
double temp = 0;
int a = 0;
while(t -> courseArray[a] != NULL)
{
temp = temp + courseGetGrade(t -> courseArray[a]);
a++;
}
return (temp / a);
}
但我似乎无法将参数 t -> courseArray[a] 传递给函数 courseGetGrade。我对指针以及应该如何实现它有点困惑,我只是不明白为什么它不能像现在这样工作。courseArray 是 Course 结构的数组,数组末尾有一个 NULL 指针。
我收到警告“从不兼容的指针类型传递“courseGetGrade”的参数 1”。如果我尝试在参数之前添加“const”,警告会变为错误:“const”之前的预期表达式。
我正在使用普通的C。
非常感谢所有帮助!
编辑。这是完整的编译器输出。与我最初发布的代码相比,完整输出中的功能更多,因此警告也更多:
transcript.c: In function âtsAverageGradeâ:
transcript.c:66: warning: passing argument 1 of âcourseGetGradeâ from incompatible pointer type
course.h:27: note: expected âconst struct Course *â but argument is of type âstruct Course *â
transcript.c: In function âtsSetCourseArrayâ:
transcript.c:89: error: invalid application of âsizeofâ to incomplete type âstruct Courseâ
transcript.c:94: warning: assignment from incompatible pointer type
transcript.c: In function âtsPrintâ:
transcript.c:114: warning: passing argument 1 of âcourseGetNameâ from incompatible pointer type
course.h:24: note: expected âconst struct Course *â but argument is of type âstruct Course *â
transcript.c:114: warning: passing argument 1 of âcourseGetGradeâ from incompatible pointer type
course.h:27: note: expected âconst struct Course *â but argument is of type âstruct Course *â
transcript.c: In function âtsCopyâ:
transcript.c:126: warning: passing argument 2 of âtsSetCourseArrayâ from incompatible pointer type
transcript.c:80: note: expected âstruct Course **â but argument is of type âstruct Course ** constâ
Edit.2 这是导致第 89 行错误的函数:
void tsSetCourseArray(Transcrpt *t, Course **courses)
{
assert(t && courses);
free(t -> courseArray);
int a = 0;
while(courses[a] != NULL)
a++;
t -> courseArray = malloc(sizeof(struct Course) * (a+1));
a = 0;
while(courses[a] != NULL)
{
t -> courseArray[a] = courseConstruct(courseGetName(courses[a]), courseGetGrade(courses[a]));
a++;
}
t -> courseArray[a] = NULL;
}