| | |
Pointer arrays and structures
Thread Solved |
•
•
Join Date: Aug 2008
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);
•
•
Join Date: Aug 2008
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:
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
students[i] = (studentInfo*) malloc( sizeof(studentInfo) );
•
•
Join Date: Aug 2008
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 4:16 pm.
![]() |
Similar Threads
- 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()
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o ide inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h





