fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX_LEN 80
  5.  
  6. // **** Add a constant for the size of name
  7. #define MAX_NAME 100
  8.  
  9. struct Student {
  10. char name[MAX_NAME]; // ** Save the hassle using malloc
  11. int id; // student number
  12. char enroll; // *** Just need a character - not a character pointer
  13. };
  14.  
  15. struct Student student1;
  16.  
  17. void getStudent(struct Student *s)
  18. {
  19. printf("Type the name of the student: ");
  20. // *** Do not need malloc as using an array
  21. // s->name = malloc(100); // assume name has less than 100 letters
  22. fgets(s->name, MAX_NAME, stdin);
  23.  
  24. printf("\nType the student number: "); // ** Corrected a typo
  25. scanf("%d", &(s->id)); // *** You should check the return value here and take appropriate action - I leave that you the reader
  26.  
  27. printf("\nType the student enrollment option (D or X): ");
  28. scanf(" %c", &(s->enroll)); // scanf requires a charcter pointer here
  29. // return; This is not needed
  30. }
  31.  
  32. void printStudent(struct Student s)
  33. {
  34. // *** THIS CODE DOES NOT MAKE ANY SENSE
  35. // char name[MAX_LEN];
  36. // char enroll[MAX_LEN];
  37. // int id;
  38.  
  39. // s.id = id;
  40. // s.name = name;
  41. // s.enroll = enroll;
  42.  
  43. printf("Student Details: %d %s %c \n", s.id, s.name, s.enroll );
  44. /// return; Not needed
  45. }
  46.  
  47. int main(int argc, char *argv[]){
  48. getStudent(&student1);
  49. printStudent(student1);
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 10320KB
stdin
name
1 Q
stdout
Type the name of the student: 
Type the student number: 
Type the student enrollment option (D or X): Student Details: 1 name
 Q