array of pointers of a struct in C -


i'm having bad time pointers , arrays , need help.

there exercise have struct:

typedef struct student_node{   unsigned number;   const char *name;   unsigned class;   struct student_node *next; } studentnode; 

and have implement function:

void groupstudentbyclass(studentnode *classes[], studentnode students[], size_t num_students) 

my problem want change number in classes , print value , can't. gives me "segmentation fault(core dumped)". don't understand why...

this test code:

size_t nclasses=3; studentnode *classes [nclasses]; classes[0]->number=0; printf("%u\n",classes[0]->number); 

btw can't use malloc , things allocate memory.

@matthias has explained why getting segv.

if cannot use malloc need preallocate structures. like

const size_t max_student = 1024; studentnode studentpool[max_student]; int studentlast = -1; 

then need allocation routine

studentnode* studentget() {     if (++studentlast >= max_student)     {         /* print error message , exit */     }     return studentpool[studentlast]; } 

next, when need allocate

classes[0] = studentget(); 

then can assignments , prints.

you may need write routines free structures , return them pool.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -