473,698 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1769
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
1502
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 .compactframework thread, but nobody helped. main problem is that I have tried all the workarounds I have found in the Inet but nothing worked... I tried passing IntPtr, Byte Arrays, Serialisation, even used OpenNETCF, but
4
3858
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 that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
8
54442
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 { int x,y,z; char a,b,c;
1
1771
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 to files as a large byte array. This is a series of Lectures where there is a capacity for 8 instructors with up to 8 lectures each. So a parameters file made from glctInstTable is 2,232 bytes. The 64 lectures, for the above, each consist of...
16
4817
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 keep track on the structures... But it's a bit confusing - my program won't compile and I don't know what to do about the warnings/error messages. c:\documents and settings\dell\Desktop\test\main.c(5) : warning
7
3163
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 thorughout the course the programs use so it will need to
12
3878
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
44
5781
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
4686
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
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.