0

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;
}
3
  • Could you post all compiler output. Commented Jun 23, 2012 at 21:15
  • Here it is. There are more functions and therefore more warnings in the full output than I originally posted: Commented Jun 23, 2012 at 21:22
  • 1
    The . and -> operators bind very tightly; do not use spaces around them. Commented Jun 23, 2012 at 22:12

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! Warnings are now fixed, however the error: invalid application of "sizeof" to incomplete type "struct Course" persist. Any ideas?
@JonasNordfors, the same thing. Should be sizeof(Course).
I changed it and now I get the error: invalid application of "sizeof" to incomplete type "Course"
@JonasNordfors, that should work as Course is fully defined, see ideone.com/caGjj .
I don't know why it doesn't work, however I changed it to sizeof(courses[a]) and now it compiled, but I'm getting a lot of valgrind errors when running it. I'm guessing that I need to realloc more memory for each individual course struct in the array. Thanks for your help, I'm going to try to fix it tomorrow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.