[英]passing an argument ( a struct in an array of structs within a struct) to a function in 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是一個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;
}
更改:
typedef struct Transcript_s
{
char* name;
struct Course** courseArray;
} Transcript;
至:
typedef struct Transcript_s
{
char* name;
Course** courseArray; /* 'Course' is a typedef for 'struct Course_s'. */
} Transcript;
以下是不正確的,原因有兩個:
t -> courseArray = malloc(sizeof(struct Course) * (a+1));
struct Course
應該是Course
但更重要的是它應該是Course*
因為空間需要為Course*
分配Course*
: t->courseArray
是Course**
。 改成:
t -> courseArray = malloc(sizeof(Course*) * (a+1));
另外,以下內容不會釋放courseArray
的Course
實例,它只會釋放指針數組:
free(t -> courseArray);
你需要遍歷courseArray
並釋放每個單獨的元素,然后釋放指針數組:
while (t->courseArray[a] != NULL)
{
free(t->courseArray[a]->name); /* If name was dynamically allocated. */
free(t->courseArray[a]);
}
free(t -> courseArray);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.