lundi 2 mars 2015

How can I initialize and dynamically allocate an int pointer member in an array of structs?



How can I initialize and dynamically allocate an int pointer that is in an array of structs?


My program allows me to print enroll[0].grades[x], but when I try to access any other index value of enroll other than 0 (such as enroll[1].grades[x] or enroll[2].grades[x]), my program segmentation faults.


Here's my code...


How can I make enroll[2].grades[x], for example, initialize and equal zero?


In my struct:



struct Enroll
{
int *grades;
};


In main:



struct Enroll *enroll = (struct Enroll *) calloc(count.enroll, 10 * sizeof(struct Enroll));

enroll->grades = (int *) calloc(count.grades, 10 * sizeof(int));


In functions:



enroll[x].grades[y];



Aucun commentaire:

Enregistrer un commentaire