![]() |
| ||
| Pointer arrays and structures Hey all, I am a C beginner. My problem with my code is that I get a segfault. Here's my code #include <stdio.h>According to gdb the segfault happens on: scanf(" %d", &students[i]->totalClasses);But I don't know what I am doing wrong. Any help? |
| ||
| Re: Pointer arrays and structures You have allocated space for all of the studentInfo pointers here (except the last one, you should not be subtracting 1): struct studentInfo *students[numOfStudents-1]; But, you have not allocated space for the actual studentInfo structures that each pointer will point to. To do that you will have to make a loop that allocates a studentInfo for each pointer using malloc(). Something like: students[i] = (studentInfo*) malloc( sizeof(studentInfo) ); |
| ||
| Re: Pointer arrays and structures Ahh! I see, so you need to allocate space for each structure that the pointer points to. I ended up using: students[i] = (struct studentInfo *)malloc(sizeof(struct studentInfo)); And now everything works fine. Thank you. :) |
| All times are GMT -4. The time now is 7:58 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC