View Single Post
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