| | |
Pointer arrays and structures
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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()
Views: 427 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi





