main()
This should be int main(void)
.
{
Minion *dummy = (Minion *)malloc(sizeof(Minion));
This is more clearly and reliably written as:
Minion *dummy = malloc(sizeof *dummy);
...
dummy->father = &dummy;
dummy->home = 0;
That's ok.
printf("%d %d %d", &dummy, dummy->father, (dummy->father)->father);
You have several problems here.
dummy
is a pointer object. &dummy
is the address of that pointer object. The pointer object is allocated on the stack, and there's no particular reason to care about its address.
If you want to print the address of the Minion
object you allocated, change &dummy
to just dummy
; you want the value of the pointer object.
%d
is used only for printing values of type int
, not pointers. To print a pointer value, use %p
, which expects an argument of type void*
, so in general you'll have to convert the pointer value to void*
before printing it. That line should look like this:
printf("%p %p %p\n",
(void*)dummy,
(void*)dummy->father,
(void*)dummy->father->father);
Note that I've added a \n
to the end of the line so it will print properly. I've also removed an unnecessary set of parentheses.
}
One more minor point, not relevant to your problem. It's common to define a typedef
for each struct
type -- but it's not necessary. My own personal preference is to omit the typedef
and refer to the structure type by its original name -- in this case, struct minion
. It's simpler (since it's easier to refer to the type before it's been completely defined). It's a little more typing, but IMHO the code is clearer if struct types are visibly structures. (It makes sense to use a typedef if you want the type to be opaque, so that code using the type doesn't know that it's a structure. You're not doing that here.)
On the other hand, plenty of C programmers prefer to use typedefs for structures because it provides a name for the type that's a single identifier. Pick a style and be consistent.