if you copied this from a textbook, naughty textbook.
if you wrote this yourself, naughty you
using foo, bar, p as variable names when you are trying to learn something just adds a layer of complexity that doesnt need to be there. ive rewritten the example so that it makes sense.
i changed 2 things. firstly where you define the struct. now, when i was taught about structures, we would always define them before main. so ive made that change. if you define a structure inside of main, then the structure definition would be local to the main function (that is, no one else but main can see the struct) which is why you get an error on your proof function. proof doesnt know what a 'struct foo' is. you have to define structures before you can write functions which use them.
the second thing is your line 'proof(&p)'. what this does is it passes the address of the variable p and gives it to the function. what we actually want is the address of bar, which is already stored in p. so the correct line is just 'proof(p);' without ampersand.
here is the code with my changes.
Code:
#include <stdio.h>
struct animal {
char *name;
int legs;
};
void goToTheVet(struct animal* sickPet) {
printf("hello %s. your %i legs are all healthy\n",
sickPet->name, sickPet->legs);
}
int main(int argc, char* argv[]){
struct animal myCat;
struct animal *pointerToAnimal;
pointerToAnimal = &myCat;
pointerToAnimal->name = "Mr. Snuggles";
pointerToAnimal->legs = 4;
goToTheVet(pointerToAnimal);
// this works too
// goToTheVet(&myCat);
return (0);
}