1

i have the following structure:

typedef struct Course {
int course_id;
char* course_name;
int prior_course_id;
StudentTree* students;
} Course;

and the following function i need to implement:

void createReport(FILE* courses[], int numOfCourses, FILE* studentFile, char* reportFileName

as you can see i get an array of FILE*, each cell contains different file pointer.

my intention is to create an array that each cell is Course* type, and initialize each cell with a Course struct containing the data read from the courses files.

what is the correct way to declare it inside the function? do i need to dynamically allocate memory for it, or it can be done in compilation?

i've tried

Course* course_array[numOfCourses] = {NULL};
Course* course_array[numOfCourses] = NULL;

but it won't compile.

thanks for your help

4 Answers 4

1

You declare an array of structs the same way you declare an array of ints or FILE *s:

Type variableName[numberOfElements];

Before C99 (and barring compiler specific extensions), creating an array with a variable number of elements on the stack wasn't supported. So make sure that you are targeting the correct standard. In your case, assuming C99 support, the following should work:

Course *course_array[numOfCourses];

Because you intend to initialize each of the elements in the array, there is no need to zero them out.

You would then access the elements like this:

course_array[0] = malloc(sizeof(Course))
course_array[0]->course_id = 2;
/* etc. */

Now if you can't assume C99 support, things get a bit more tricky but not much:

Course *course_array = malloc(sizeof(Course *) * numOfCourses);

After that you can access course_array with the same array notation:

course_array[0] = malloc(sizeof(Course))
course_array[0]->course_id = 42;
/* etc. */

Once you're doing with the array, you'll need to make sure that you free any of the memory that you allocated:

for (i = 0; i < numOfCourses; i++) {
    free(course_array[i]);
}

/* If you malloc'd course_array, then you need this too */
free(course_array);
Sign up to request clarification or add additional context in comments.

3 Comments

thank you. without the = { NULL} there is no compilation error.
Just now noticed your edit. what is C99 support? i don't think i have this, but when defining like you suggested : " Course *course_array[numOfCourses]; " with only allocating memory for every cell ( and not for the array pointer itself like in the 4th code block in your comment ), everything went fine, compilation and runtime
It's the second most recent C standard (C11 is the most current): en.wikipedia.org/wiki/C99
1
Course* course_array[numOfCourses] = {NULL};

This is good, but it creates array of Course *. So you need to allocate memory for each pointer in course_array before accessing it.

Something like

course_array[0] = malloc(sizeof(Course));
course_array[0]->course_id = someid;

2 Comments

I know it's obvious but mention that the memory should be freed after work is done.
Oh great. my memory allocation is in other function so in a loop i do: course_array[i] = getCourseProblematicStudents(courses[i], grades); But with the = { NULL } ; i get compilation error ../src/Exercise3.c:254:2: warning: excess elements in array initializer [enabled by default] ../src/Exercise3.c:254:2: warning: (near initialization for ‘course_array’) [enabled by default] only when removing the = { NULL } it goes ok thank you
0

When you define the array in the first place, you shouldn't need to allocate memory. You're defining the array on the stack, and the elements of the array are just pointers.

I think what you should do is first define the array, and then initialize each element with a malloc call. For example:

Course* course_array[numOfCourses];
for(int i = 0; i < numOfCourses, i++) {
     course_array[i] = (Course*)malloc(sizeof(Course));

Comments

0

My favorite way:

typedef struct {
    int a;
    char b;
    float c;
}DATA;

//then use typdef'ed DATA to create array (and a pointer to same) 

DATA data[10], *pData; 

//then, in function, you can initialize the pointer to first element of array this way:  

int main(void)
{
    pData = &data[0];
    return 0;
}

Your example code would look like this:

typedef struct {
    int course_id;
    char* course_name;
    int prior_course_id;
    StudentTree* students;
} COURSE;  
//then in function:  

COURSE course[numOfCourses]

Comments

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.