| | |
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 ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency function givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





