fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4. #define MaxLen 1000
  5. typedef struct
  6. {
  7. int age;
  8. char * name;
  9. } student;
  10.  
  11. typedef enum Result {aged,named,success} Result;
  12. Result res = success;
  13. student ** myFunc()
  14. {
  15. student ** ans = (student **)malloc(sizeof(student *));
  16. int numStudents = 0, currentAge, flag = 1;
  17. student * std;
  18. while (flag)
  19. {
  20. std = (student *)malloc(sizeof(student));
  21. std->name = (char *) malloc(sizeof(char) * MaxLen);
  22. scanf("%s", std->name);
  23. if (strcmp(std->name,"enough") != 0)
  24. {
  25. scanf("%d", &currentAge);
  26. if (currentAge >= 0)
  27. {
  28. std->age = currentAge;
  29. numStudents++;
  30. ans = (student **) realloc(ans, sizeof(student*) * numStudents);
  31. ans[numStudents-1] = std;
  32. }
  33. else
  34. {
  35. res = aged;
  36. free(std->name);
  37. free(std);
  38. break;
  39. }
  40. }
  41. else
  42. {
  43. res = named;
  44. free(std->name);
  45. free(std);
  46. break;
  47. }
  48. }
  49.  
  50. if (numStudents == 0)
  51. {
  52. free(ans);
  53. return NULL;
  54. }
  55. return ans;
  56. }
  57. int main(void) {
  58. student ** arr = myFunc();
  59. if (res == aged)
  60. {
  61. printf("I stopped because of age\n");
  62. }
  63. else if (res == named)
  64. {
  65. printf("I stopped because of names\n");
  66. }
  67. else
  68. {
  69. printf("I stopped becuase of allocation failure\n");
  70. }
  71. int num, i;
  72. student * std;
  73. num = sizeof(arr)/sizeof(student *);
  74. for (i=0;i<num;i++)
  75. {
  76. std = arr[i];
  77. printf("Student %s is of age %d", std->name, std->age);
  78. }
  79. return 0;
  80. }
  81.  
Time limit exceeded #stdin #stdout 5s 8187904KB
stdin
Standard input is empty
stdout
Standard output is empty