我有这方面的结构“过程”和功能:传递参数(在一个结构中的结构的数组的结构体)的函数用C
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是一个课程结构数组,在数组末尾有一个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;
}
您能发布所有编译器输出吗? – hmjd
它在这里。在我输入的完整输出中有更多的函数,因此会有更多的警告: –
'.'和' - >'运算符绑定得非常紧密;不要在他们周围使用空格。 –