fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_NAME_LENGTH (10)
  6.  
  7. typedef struct FooImpl* Foo;
  8.  
  9. typedef struct FooImpl {
  10. int Number;
  11. int Score;
  12. char* Name;
  13. } FooImpl;
  14.  
  15. Foo Foo_Init(int Number, int Score, const char* Name)
  16. {
  17. Foo f = (Foo)malloc(sizeof(FooImpl));
  18. f->Number = Number;
  19. f->Score = Score;
  20. if (strlen(Name) > MAX_NAME_LENGTH) {
  21. free(f);
  22. return NULL;
  23. }
  24. f->Name = (char*)malloc(strlen(Name)+1);
  25. strcpy(f->Name, Name);
  26. return f;
  27. }
  28.  
  29. void Foo_Delete(Foo f)
  30. {
  31. if (!f)
  32. return;
  33. free(f->Name);
  34. free(f);
  35. }
  36.  
  37. void Foo_Print(Foo f, const char* Name)
  38. {
  39. if (strncmp(f->Name, Name, MAX_NAME_LENGTH) != 0)
  40. return;
  41. fprintf(stdout, "Number: %i, Score: %i, Name: %s", f->Number, f->Score, f->Name);
  42. }
  43.  
  44. int main(void)
  45. {
  46. Foo f[2];
  47. int i;
  48. char input[MAX_NAME_LENGTH];
  49.  
  50. f[0] = Foo_Init(1, 10, "Yamada");
  51. f[1] = Foo_Init(2, 20, "Yoshida");
  52.  
  53. scanf("%s", input);
  54.  
  55. for (i=0; i<sizeof(f)/sizeof(Foo); i++) {
  56. Foo_Print(f[i], input);
  57. Foo_Delete(f[i]);
  58. }
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 2428KB
stdin
Yoshida
stdout
Number: 2, Score: 20, Name: Yoshida