0

我有一个结构“课程”和一个函数:

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;
}
4

1 回答 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->courseArrayis a分配空间Course**。改成:

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

此外,以下内容不会释放 中的Course实例courseArray,它只会释放指针数组:

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);
于 2012-06-23T21:24:26.573 回答