2012-06-23 82 views
0

我有這方面的結構「過程」和功能:傳遞參數(在一個結構中的結構的數組的結構體)的函數用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; 
} 
+0

您能發佈所有編譯器輸出嗎? – hmjd

+0

它在這裏。在我輸入的完整輸出中有更多的函數,因此會有更多的警告: –

+1

'.'和' - >'運算符綁定得非常緊密;不要在他們周圍使用空格。 –

回答

2

變化:

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*t->courseArrayCourse**。更改爲:

t -> courseArray = malloc(sizeof(Course*) * (a+1)); 

此外,以下將不釋放在courseArrayCourse情況下,它只會釋放指針數組:

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); 
+0

謝謝!警告現在已經修復,但是錯誤:「sizeof」對不完整類型「struct Course」的無效應用仍然存在。有任何想法嗎? –

+0

@JonasNordfors,同樣的事情。應該是'sizeof(Course)'。 – hmjd

+0

我改變了它,現在我得到的錯誤:「sizeof」到不完整類型「課程」的無效申請 –