RSS Forums RSS

Pointer arrays and structures

Please support our C advertiser: Programming Forums
Thread Solved
Reply
Posts: 19
Reputation: OutOfReach is an unknown quantity at this point 
Solved Threads: 2
OutOfReach OutOfReach is offline Offline
Newbie Poster

Pointer arrays and structures

  #1  
Nov 28th, 2008
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?
AddThis Social Bookmark Button
Reply With Quote  
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Pointer arrays and structures

  #2  
Nov 28th, 2008
You have allocated space for all of the studentInfo pointers here (except the last one, you should not be subtracting 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:
students[i] = (studentInfo*) malloc( sizeof(studentInfo) );
Reply With Quote  
Posts: 19
Reputation: OutOfReach is an unknown quantity at this point 
Solved Threads: 2
OutOfReach OutOfReach is offline Offline
Newbie Poster

Re: Pointer arrays and structures

  #3  
Nov 28th, 2008
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 3:16 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 383 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:27 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC