Michael Mair wrote:
Chad schrieb:
The following question stems from the following thread on comp.lang.c:
http://groups.google.com/group/comp....20260b30952fe7
I was sort of mystified by a comment made by Eric. And I quote:
Providing context:
struct my_struct{
struct my_struct *ptr;
};
Where the question was, why
struct mystruct *foo;
struct mystruct bar;
foo->ptr= &bar;
does not work.
"You might also be confused by the fact that each
`struct my_struct' contains a pointer to a `struct my_struct'
object. There is nothing special about that inner pointer:
it's just a pointer. It could perfectly well be a pointer
to some completely different type of object -- it could just
as well be a `double'. The point (sorry) is that you cannot
get at that inner datum until you have a `struct my_struct'
that holds it."
So would an inner pointer of
struct mystruct {
int data;
};
struct mystruct new;
be 'internally' represented as:
struct my_struct *inner_pointer = &new;
No. You misunderstood the thing. The "inner" pointer is the
ptr member of foo, the "outer" pointer is foo itself.
Eric thought that because ptr and foo are of the same type,
that there might be more confusion than in the
struct my_struct{
struct my_other_struct *ptr;
};
case. In both cases, you must assign the address of
appropriate storage for at least one struct my_struct object
to foo before you can assign anything to ptr.
Yes, that's what I was getting at. When someone asks a
question that shows he hasn't quite grasped something, the
task for the answerer is to try to figure out where his grip
slipped away. I wasn't sure just where the OP's hand had
missed the handle, so I made two stabs at what seemed to be
likely possibilities. One of those was that the OP might be
baffled or at least distracted by the struct containing a
pointer to its own type (something that has baffled others
before him), so I was trying to explain the distinction between
the container and the contained, and why the contained pointer
wouldn't work until its container was located.
No mystery, really: just an armchair diagnosis of where
the OP's hangup might have been. Usually such diagnoses are
not 100% accurate; the hope is that they're close enough to
cause the "Aha!" reaction in the questioner.
--
Eric Sosman
es*****@acm-dot-org.invalid