473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

A structures inner pointer

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

Mar 4 '06 #1
2 1760
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.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Mar 4 '06 #2
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
Mar 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ivan | last post by:
Hi All, I have a problem with marshaling complex structures (containing numbers, strings, arrays of another structures) to native C function in dll. I have already posted same question to...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
8
by: Frank Münnich | last post by:
Hi there.. My name is Frank Münnich. I've got a question about pointers that refer to an array of a structure. How do I declare that type? If I have declared a structure struct mystruc {...
1
by: Galen Somerville | last post by:
And yet another VB6 to VB2005 problem. All helpful suggestions appreciated. As you can see in the code below, my structures use fixed length strings and known array sizes. Consequently I can save...
16
by: Martin Joergensen | last post by:
Hi, I wanted to try something which I think is a very good exercise... I read in data from the keyboard and store them in a structure. There's a pointer called "data_pointer" which I use to...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.