简体   繁体   中英

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. 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.

I get a warning "passing argument 1 of "courseGetGrade" from incompatible pointer type". If I try adding "const" before the argument the warning changes to an error: expected expression before "const".

I'm using plain 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:

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** . 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:

free(t -> courseArray);

You need to iterate over courseArray and free each individual element and then free the array of pointers:

while (t->courseArray[a] != NULL)
{
    free(t->courseArray[a]->name); /* If name was dynamically allocated. */
    free(t->courseArray[a]);
}
free(t -> courseArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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