Pointer arrays and structures
Please support our C advertiser: Programming Forums
Thread Solved
![]() |
•
•
Posts: 19
Reputation:
Solved Threads: 2
Hey all, I am a C beginner. My problem with my code is that I get a segfault.
Here's my code
According to gdb the segfault happens on:
But I don't know what I am doing wrong. Any help?
Here's my code
C Syntax (Toggle Plain Text)
#include <stdio.h> struct studentInfo { int totalClasses; float GPA; char studentName[41]; }; main() { int numOfStudents, i; printf("How many students would you like to take account of? "); scanf(" %d", &numOfStudents); struct studentInfo *students[numOfStudents-1]; for (i=0; i < numOfStudents; i++) { printf("Enter the total number of the student's classes: "); scanf(" %d", &students[i]->totalClasses); printf("Enter the student's GPA: "); scanf(" %f", &students[i]->GPA); getchar(); /* Clear the extra keypress in the buffer */ printf("Enter the student's name: "); fgets(students[i]->studentName, 41, stdin); printf("\n\n"); } printf("\n\n"); for (i=0; i < numOfStudents; i++) { printf("Student Name: %s", students[i]->studentName); printf("Student GPA: %.3f\n", students[i]->GPA); printf("Number of Student Classes: %d\n\n", students[i]->totalClasses); } printf("\n\n"); printf("Goodbye.\n"); return 0; }
C Syntax (Toggle Plain Text)
scanf(" %d", &students[i]->totalClasses);
•
•
Posts: 77
Reputation:
Solved Threads: 16
You have allocated space for all of the studentInfo pointers here (except the last one, you should not be subtracting 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:
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) );
•
•
Posts: 19
Reputation:
Solved Threads: 2
Ahh! I see, so you need to allocate space for each structure that the pointer points to.
I ended up using:
And now everything works fine. Thank you.
I ended up using:
C Syntax (Toggle Plain Text)
students[i] = (struct studentInfo *)malloc(sizeof(struct studentInfo));
And now everything works fine. Thank you.
Last edited by OutOfReach : Nov 28th, 2008 at 3:16 pm.
![]() |
Similar Threads
Other Threads in the C Forum
- Dynamic array of structures (C++)
- Qsort Pointer To Arrays Of Structs (C)
- help with program using structures and printing to screen (C)
- Problem when passing a structure containing an array of 2D-chars array to a function (C)
- Dynamic Array of Structures, Loop problem! (C++)
- Passing Arrays to function in Visual C++ (C++)
- structures containing a pointer to an array (C++)
Other Threads in the C Forum
- Previous Thread: How to link two C files in linux
- Next Thread: String to Double Conversion without using strtod() or atof()
•
•
•
•
Views: 383 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)





Linear Mode