A structures inner pointer

C

Chad

The following question stems from the following thread on comp.lang.c:

http://groups.google.com/group/comp...7381a/5f20260b30952fe7?hl=en#5f20260b30952fe7

I was sort of mystified by a comment made by Eric. And I quote:


"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;

Any clarification would be greatly appreciated.

Chad
 
M

Michael Mair

Chad said:
The following question stems from the following thread on comp.lang.c:

http://groups.google.com/group/comp...7381a/5f20260b30952fe7?hl=en#5f20260b30952fe7

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.


Cheers
Michael
 
E

Eric Sosman

Michael said:
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.



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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top