2012-06-23 45 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”到不完整类型“课程”的无效申请 –

相关问题