fork download
  1. the first attempt:
  2. typedef struct Pet{
  3. char* name;
  4. int legs;
  5. char* color;
  6. } pet;
  7. void writedata(pet *Pet, size_t FIELD){
  8. char *base;
  9. char* b;
  10. base=(char*) Pet;
  11. char string[50];
  12. offset=offsetof(Pet, FIELD);
  13. b=(char *)(base+offset);
  14. gets(string);
  15.  
  16. *b/*(char*)Pet+offsetof(Pet, FIELD)*/ = (char*)malloc(strlen(string)+1);
  17. strcpy(*b, string);
  18. }
  19. void addpet(pet* Pet, int &TotalLegs){
  20. puts("Input name\n");
  21. writedata(Pet, offsetof(Pet, name));//how I actually wanted it to work
  22. writedata(Pet, offsetof(Pet, color));
  23. ___________________________________________________
  24.  
  25. the second attempt:
  26. typedef struct Pet{
  27. char* name;
  28. int legs;
  29. char* color;
  30. } pet;
  31. void writedata(pet *Pet, size_t FIELD){
  32. char string[50];
  33. gets(string);
  34. (char*)Pet+offsetof(struct Pet, FIELD) = (char*)malloc(strlen(string)+1);
  35. strcpy((char*)Pet+FIELD, string);
  36. }
  37.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:2: error: unknown type name ‘the’
  the first attempt:
  ^
prog.c:1:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘attempt’
  the first attempt:
            ^
prog.c:6:7: warning: data definition has no type or storage class [enabled by default]
     } pet;
       ^
prog.c:6:7: warning: type defaults to ‘int’ in declaration of ‘pet’ [-Wimplicit-int]
prog.c:7:16: error: expected declaration specifiers or ‘...’ before ‘pet’
 void writedata(pet *Pet, size_t FIELD){
                ^
prog.c:7:26: error: unknown type name ‘size_t’
 void writedata(pet *Pet, size_t FIELD){
                          ^
prog.c:19:13: error: expected declaration specifiers or ‘...’ before ‘pet’
 void addpet(pet* Pet, int &TotalLegs){
             ^
prog.c:19:27: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
 void addpet(pet* Pet, int &TotalLegs){
                           ^
stdout
Standard output is empty