943,758 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 571
  • C RSS
Nov 28th, 2008
0

Pointer arrays and structures

Expand Post »
Hey all, I am a C beginner. My problem with my code is that I get a segfault.
Here's my code
  1. #include <stdio.h>
  2.  
  3. struct studentInfo {
  4. int totalClasses;
  5. float GPA;
  6. char studentName[41];
  7. };
  8.  
  9.  
  10. main()
  11. {
  12. int numOfStudents, i;
  13. printf("How many students would you like to take account of? ");
  14. scanf(" %d", &numOfStudents);
  15.  
  16. struct studentInfo *students[numOfStudents-1];
  17.  
  18. for (i=0; i < numOfStudents; i++)
  19. {
  20. printf("Enter the total number of the student's classes: ");
  21. scanf(" %d", &students[i]->totalClasses);
  22.  
  23. printf("Enter the student's GPA: ");
  24. scanf(" %f", &students[i]->GPA);
  25. getchar(); /* Clear the extra keypress in the buffer */
  26.  
  27. printf("Enter the student's name: ");
  28. fgets(students[i]->studentName, 41, stdin);
  29. printf("\n\n");
  30.  
  31.  
  32. }
  33.  
  34. printf("\n\n");
  35.  
  36. for (i=0; i < numOfStudents; i++)
  37. {
  38. printf("Student Name: %s", students[i]->studentName);
  39. printf("Student GPA: %.3f\n", students[i]->GPA);
  40. printf("Number of Student Classes: %d\n\n", students[i]->totalClasses);
  41. }
  42. printf("\n\n");
  43.  
  44. printf("Goodbye.\n");
  45.  
  46. return 0;
  47. }
According to gdb the segfault happens on:
  1. scanf(" %d", &students[i]->totalClasses);
But I don't know what I am doing wrong. Any help?
Reputation Points: 10
Solved Threads: 2
Newbie Poster
OutOfReach is offline Offline
19 posts
since Aug 2008
Nov 28th, 2008
0

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):
  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:
  1. students[i] = (studentInfo*) malloc( sizeof(studentInfo) );
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Nov 28th, 2008
0

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:
  1. 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.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
OutOfReach is offline Offline
19 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to link two C files in linux
Next Thread in C Forum Timeline: String to Double Conversion without using strtod() or atof()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC