Pointer arrays and structures

Thread Solved
Reply

Join Date: Aug 2008
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

 
0
  #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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
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

 
0
  #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):
  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) );
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
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

 
0
  #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 4:16 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC