Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <malloc.h>
  2.  
  3. #define MAX_STUDENTS 30
  4.  
  5. typedef struct
  6. {
  7. int id;
  8. /* more fields here */
  9. } Student;
  10.  
  11. Student *studentDb[MAX_STUDENTS] = { 0 };
  12.  
  13. Student *AddStudent(int id /* more params here */)
  14. {
  15. size_t index = 0;
  16. for (; index < MAX_STUDENTS && studentDb[index]; index++)
  17. ;
  18.  
  19. studentDb[index] = malloc(sizeof(Student));
  20. studentDb[index]->id = id;
  21.  
  22. return studentDb[index];
  23. }
  24.  
  25. Student *DeleteStudent(int id)
  26. {
  27. for (size_t index = 0; index < MAX_STUDENTS; index++)
  28. {
  29. if (studentDb[index] && (studentDb[index]->id == id))
  30. {
  31. free(studentDb[index]);
  32. studentDb[index] = NULL;
  33. }
  34. }
  35.  
  36. return NULL;
  37. }
  38.  
  39. Student *GetStudent(int id)
  40. {
  41. for (size_t index = 0; index < MAX_STUDENTS; index++)
  42. {
  43. if (studentDb[index] && (studentDb[index]->id == id))
  44. {
  45. return studentDb[index];
  46. }
  47. }
  48.  
  49. return NULL;
  50. }
  51.  
  52. void CleanStudentDb()
  53. {
  54. for (size_t index = 0; index < MAX_STUDENTS; index++)
  55. {
  56. if (studentDb[index])
  57. {
  58. free(studentDb[index]);
  59. studentDb[index] = NULL;
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement