我有這方面的結構「過程」和功能:傳遞參數(在一個結構中的結構的數組的結構體)的函數用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
它在這裏。在我輸入的完整輸出中有更多的函數,因此會有更多的警告: –
'.'和' - >'運算符綁定得非常緊密;不要在他們周圍使用空格。 –