简体   繁体   English

将参数(结构中的结构数组中的结构)传递给C中的函数

[英]passing an argument ( a struct in an array of structs within a struct) to a function in C

I have a struct "course" and a function for it: 我有一个结构“课程”和一个功能:

typedef struct Course_s
    {
    char* name;
    int grade;
    } Course;

int courseGetGrade(Course const* course)
    {
    assert(course);
    return course -> grade;
    }

and another struct "transcript" and a function: 和另一个结构“成绩单”和功能:

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

But I cannot seem to pass the argument t -> courseArray[a] to the function courseGetGrade. 但我似乎无法将参数t - > courseArray [a]传递给函数courseGetGrade。 I'm a little confused with pointers and how this should be implemented, I just don't see why it doesn't work the way it is. 我对指针有点困惑,应该如何实现,我只是不明白为什么它不会像现在这样工作。 The courseArray is an array of Course structs, with a NULL pointer at the end of the array. courseArray是一个Course结构数组,在数组末尾有一个NULL指针。

I get a warning "passing argument 1 of "courseGetGrade" from incompatible pointer type". 我得到一个警告“从不兼容的指针类型”传递“courseGetGrade”的参数1。 If I try adding "const" before the argument the warning changes to an error: expected expression before "const". 如果我尝试在参数之前添加“const”,则警告会更改为错误:“const”之前的预期表达式。

I'm using plain C. 我正在使用普通C.

All help is much appreciated! 非常感谢所有帮助!

Edit. 编辑。 Here is the full compiler output. 这是完整的编译器输出。 There are more functions and therefore more warnings in the full output than in the code I originally posted: 完整输出中有更多功能,因此警告比我最初发布的代码更多:

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 Here is the function causing the error in line 89: 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;
}

Change: 更改:

typedef struct Transcript_s
{
    char* name;
    struct Course** courseArray;
} Transcript;

to: 至:

typedef struct Transcript_s
{
    char* name;
    Course** courseArray; /* 'Course' is a typedef for 'struct Course_s'. */
} Transcript;

Also the following is incorrect, for two reasons: 以下是不正确的,原因有两个:

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

struct Course should be Course but more importantly it should be Course* as space needs to be allocated for the Course* : t->courseArray is a Course** . struct Course应该是Course但更重要的是它应该是Course*因为空间需要为Course*分配Course*t->courseArrayCourse** Change to: 改成:

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

Also, the following will not free the Course instances in the courseArray , it will only free the array of pointers: 另外,以下内容不会释放courseArrayCourse实例,它只会释放指针数组:

free(t -> courseArray);

You need to iterate over courseArray and free each individual element and then free the array of pointers: 你需要遍历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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM