fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_NAME 50
  4.  
  5. struct students
  6. {
  7. char name[MAX_NAME];
  8. float average;
  9. };
  10.  
  11. void storeStudents(struct students *lst, int n);
  12. void printStudents(struct students *lst, int n);
  13. void freeStudents(struct students *lst);
  14.  
  15. int main()
  16. {
  17. int n;
  18. printf("How many students you wanna store? ");
  19. scanf("%d", &n);
  20. struct students *list;
  21. list = (struct students *)malloc(n*sizeof(struct students));
  22.  
  23. storeStudents(list,n);
  24. printStudents(list,n);
  25. freeStudents(list);
  26.  
  27. return 0;
  28. }
  29.  
  30. void storeStudents(struct students *lst, int n)
  31. {
  32. int i;
  33. for(i=0;i<n;i++)
  34. {
  35. printf("Name of student: ");
  36. scanf("%s", &(lst[i].name)); //In C arrays are not assignable, so why is this line working?
  37. printf("Average of student: ");
  38. scanf("%f", &(lst[i].average));
  39. }
  40. printf("\n");
  41. }
  42.  
  43. void printStudents(struct students *lst, int n)
  44. {
  45. int i;
  46. for(i=0;i<n;i++)
  47. {
  48. printf("Name: %s\tAverage: %.2f", lst[i].name, lst[i].average);
  49. printf("\n");
  50. }
  51. }
  52.  
  53. void freeStudents(struct students *lst)
  54. {
  55. free(lst);
  56. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘storeStudents’:
prog.c:36:17: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[50]’ [-Werror=format=]
         scanf("%s", &(lst[i].name)); //In C arrays are not assignable, so why is this line working?
                 ^
cc1: all warnings being treated as errors
stdout
Standard output is empty