Question

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

Was it helpful?

Solution

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

OTHER TIPS

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;

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

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top