🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2353

Re: how do I typedef an array of pointers to a structure?

#2353
From: Brian V. McGroarty
Date: Sat, 23 Mar 1996 06:43
87 lines
3600 bytes
Timur Tabi wrote:
>Ok, I've changed things a little bit.
>
>This is the code I have now:
>
>class NODE;
>
>typedef NODE *PNODE;                // PNODE is a pointer to a node
>typedef PNODE NODE_LINKS[];         // NODE_LINKS is an array of pointers
>to
>nodes
>typedef NODE_LINKS *PNODE_LINKS;    // PNODE_LINKS is a pointer to a
>NODE_LINK
>typedef PNODE_LINKS NODE_SLOTS[];   // NODE_SLOTS is an array of
>PNODE_LINKS
>typedef NODE_SLOTS *PNODE_SLOTS;    // PNODE_SLOTS is a pointer to a
>NODE_SLOT

You have created NODE *((*((*name)[]))[]) 


>Unfortunately, this setup gives me really screwy code.  I'm tempted to
>throw
>this whole array thing out the window and just do something like
>
>typedef NODE ***PNODE_SLOTS;
>
>which is what I had way in the beginning.  I guess my real question is:
>
>If I know something is a pointer to an array of pointers to an array of
>pointers to NODE's, can I treat them as a pointer to a pointer to a pointer
>to
>a NODE?

There are two issues at hand -- first, the above is five levels deep,
whereas what you apparently intend is only three.  The second, though, is
that the above cannot be used regardless of depth.  Perhaps someone else
can explain why compilers don't complain about or whether there is a use
for deep arrays of arbitrary size?

There seems to be a common misconception about declaring an object with []
-- or "array of unknown size."  It is widely held that [] is the functional
equivalent of * because the two function identically in the case of
referencing the inner-most array.  For example;

item 6 of char bug[] can be seen via bug[5] or *(bug+5)

item (3,4) of char bug[10][] can be seen via bug[4][3] or *(bug[4]+3) or
*(*(bug+4))+3) or *(*bug+4*10+3)

..however, consider item (3,4) of char **bug, initialization implicit --
(3,4) can be seen via bug[4][3] or *(bug[4]+3) or *(*(bug+4))+3), however 
*(*bug+4*10+3) would give an unknown item -- NOT item (3,4).

The reason is this: In the case of bug[4][3], for every "row" increment
(the [4]), the compiler can guarantee that the result will be 10 chars
forward in memory as the size of the outer array is "known."  For a **
array, the size of the outer array need not be fixed at compile time, so
rather than pointing to an x*y sized array as "char bug[4][3]" would, the
** array needs to address a table of pointers to single-dimensional arrays.


It seems to me that what you want is the following:

class NODE;

/* PNODE is a pointer to a node */
typedef NODE *PNODE; 

/* PNODE_LINKS is a pointer to a NODE_LINK */
typedef PNODE *PNODE_LINKS; /* changed */

/* PNODE_SLOTS is a pointer to a NODE_SLOT */
typedef PNODE_LINKS *PNODE_SLOTS; /* changed */

Note that the [] operator may still be used to dereference the above; it
will simply change to the unknown size (or *(*bug+4)+3) ) access method
rather than the compile-time dereference ( or *(*bug + 4*10+3) ) method.




Take care!

---
Brian Valters McGroarty -- brianmcg@bix.com
phone/fax (847) 439-7714
Technical Details
Message-ID: <4j06hs$7pj@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4io1lf$ns1@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: brianmcg@interaccess.com (Brian V. McGroarty)
Newsgroups: comp.lang.c.moderated
Subject: Re: how do I typedef an array of pointers to a structure?
Date: 23 Mar 1996 00:43:08 -0600
Organization: Internet Squire
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4j06hs$7pj@solutions.solon.com>
References: <4io1lf$ns1@solutions.solon.com>
Reply-To: brianmcg@interaccess.com
NNTP-Posting-Host: solutions.solon.com
X-Newsreader: Internet Squire 1.20