fork(2) download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. typedef struct Pet{
  5. char* name;
  6. int legs;
  7. char* color;
  8. } pet;
  9. void writedata(pet *Pet, char string[], const char field[]){
  10. gets(string);
  11. Pet->field= (char*)malloc(strlen(string)+1);//I wanted it to be treated like Pet->name
  12. strcpy(Pet->field, string);
  13. }
  14. void addpet(pet* Pet, int &TotalLegs){
  15. char string[50];
  16. char string2[50];
  17. puts("Input name\n");
  18. writedata(Pet, string, name);//the actual attempt
  19. /*gets(string);
  20. Pet->name = (char*)malloc(strlen(string)+1);
  21. strcpy(Pet->name, string);*/
  22. puts("How many legs?\n");
  23. scanf("%d", &Pet->legs);
  24. fflush(stdin);
  25. puts("What does it say?\n");
  26. gets(string2);
  27. Pet->color = (char*)malloc(strlen(string)+1);
  28. strcpy(Pet->voice, string2);
  29. puts("_____\n");
  30. TotalLegs += Pet->legs;
  31. }
  32. void main(){
  33. int TotalLegs = 0;
  34. pet* Pet1 = (pet*)malloc(sizeof(pet));
  35. addpet(Pet1, TotalLegs);
  36. pet* Pet2 = (pet*)malloc(sizeof(pet));
  37. addpet(Pet2, TotalLegs);
  38. pet* Pet3 = (pet*)malloc(sizeof(pet));
  39. addpet(Pet3, TotalLegs);
  40. printf("The animals have %d legs\n", TotalLegs);
  41. }
  42.  
  43.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘writedata’:
prog.c:10:5: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
     gets(string);
     ^
prog.c:11:8: error: ‘pet’ has no member named ‘field’
     Pet->field= (char*)malloc(strlen(string)+1);//I wanted it to be treated like Pet->name
        ^
prog.c:12:15: error: ‘pet’ has no member named ‘field’
     strcpy(Pet->field, string);
               ^
prog.c: At top level:
prog.c:14:27: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
 void addpet(pet* Pet, int &TotalLegs){
                           ^
prog.c:32:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 void main(){
      ^
prog.c: In function ‘main’:
prog.c:35:2: warning: implicit declaration of function ‘addpet’ [-Wimplicit-function-declaration]
  addpet(Pet1, TotalLegs);
  ^
stdout
Standard output is empty