Thread View: comp.lang.c.moderated
6 messages
6 total messages
Started by Timur Tabi
Thu, 14 Mar 1996 02:26
how do I typedef an array of pointers to a structure?
Author: Timur Tabi
Date: Thu, 14 Mar 1996 02:26
Date: Thu, 14 Mar 1996 02:26
40 lines
1857 bytes
1857 bytes
I know this is a simple question, but I want to make sure that I get the
syntax absolutely correct. I have a structure, called NODE_LINK:
class NODE;
typedef struct {
unsigned uNumLinks;
NODE *pnode[1];
} NODE_LINK;
The second field is supposed to be an array of pointers to NODE, which is an
undefined class (or a structure, it doesn't matter for this discussion).
Question #1: did I define this field correctly? How do I make sure that it's
not interpreted as a pointer to an array of NODE's?
Actually, the second field can be an array of any length, as long as it's at
least one element long. In most cases, the array will end up being a few
elements long.
In addition, I want to have an array of pointers to NODE_LINK's. What I have
right now is:
typedef NODE_LINK *NODE_LINK_PTR;
typedef NODE_LINK_PTR NODE_LINK_ARRAY[];
This breaks it down into two steps, but I want to avoid the intermediate step.
Question #2: Does the following line produce the same definition for
NODE_LINK_ARRAY:
typedef NODE_LINK *NODE_LINK_ARRAY[];
How can I make sure that this isn't interpreted as a pointer to an array of
NODE_LINK's?
--
Timur "too sexy for my code" Tabi, timur@vnet.ibm.com
Architect, OS/2 Warp realtime MIDI subsystem
http://timur.austin.ibm.com (internal IBM only)
http://www.shirenet.com/~crusader/Timur (external)
Re: how do I typedef an array of pointers to a structure?
Author: Henning Makholm
Date: Sat, 16 Mar 1996 14:50
Date: Sat, 16 Mar 1996 14:50
35 lines
1482 bytes
1482 bytes
timur@vnet.ibm.com (Timur Tabi) writes:
>typedef struct {
> unsigned uNumLinks;
> NODE *pnode[1];
>} NODE_LINK;
>Question #1: did I define this field correctly?
Yes, given that you really want your array to have length 1 :-)
>How do I make sure that it's not interpreted as a pointer to an array
>of NODE's?
By looking in a decent C grammar and discovering that postfixes bind
tighter than prefixes, in declarators as well as in expressions.
>typedef NODE_LINK *NODE_LINK_ARRAY[];
>
>How can I make sure that this isn't interpreted as a pointer to an array of
>NODE_LINK's?
The same way. If you want pointers to arrays you must use parantheses:
sometype (*id)[LENGTH] ;
If you feel unsecure either way it's always legal to clarify what you
mean using parantheses. I.e. for your array of pointers:
NODE *(pnode[1]);
--
Henning Makholm - math and CS student - University of Copenhagen
makholm@diku.dk - http://www.diku.dk/students/makholm - fido 2:235/224.92
Re: how do I typedef an array of pointers to a structure?
Author: Kurt Watzka
Date: Sun, 17 Mar 1996 22:19
Date: Sun, 17 Mar 1996 22:19
67 lines
2586 bytes
2586 bytes
timur@vnet.ibm.com (Timur Tabi) writes:
>I know this is a simple question, but I want to make sure that I get the
>syntax absolutely correct. I have a structure, called NODE_LINK:
>class NODE;
[I will ignore this, but I will answer this question as if it had
been an answer about C. So, I will assume that this was "struct NODE;"]
>typedef struct {
> unsigned uNumLinks;
> NODE *pnode[1];
>} NODE_LINK;
>The second field is supposed to be an array of pointers to NODE, which is an
>undefined class (or a structure, it doesn't matter for this discussion).
>Question #1: did I define this field correctly? How do I make sure that it's
>not interpreted as a pointer to an array of NODE's?
By _not_ writting it as
NODE (*pnode)[1];
>Actually, the second field can be an array of any length, as long as it's at
>least one element long. In most cases, the array will end up being a few
>elements long.
In this case, you might run into problems, because the code generated
by your compiler might have severe problems accessing all but the first
element of that array. If you want what is conceptually a vector of
pointers to NODE, you _should_ use
NODE **ppnode;
instead of relying on the "array hack".
>In addition, I want to have an array of pointers to NODE_LINK's. What I have
>right now is:
>typedef NODE_LINK *NODE_LINK_PTR;
>typedef NODE_LINK_PTR NODE_LINK_ARRAY[];
>This breaks it down into two steps, but I want to avoid the intermediate step.
> Question #2: Does the following line produce the same definition for
>NODE_LINK_ARRAY:
>typedef NODE_LINK *NODE_LINK_ARRAY[];
Yes, it does. Forget about the "typedef" for a moment. Then "NODE_LINK_PTR"
is an array of unspecified size of pointers to "NODE_LINK".
>How can I make sure that this isn't interpreted as a pointer to an array of
>NODE_LINK's?
By _not_ writting it as
typedef NODE_LINK (*NODE_LINK_ARRAY)[];
Kurt
--
| Kurt Watzka Phone : +49-89-2180-6254
| watzka@stat.uni-muenchen.de
| ua302aa@sunmail.lrz-muenchen.de
Array hack (was Re: how do I typedef an array of pointers to a structure?)
Author: S. Lee
Date: Mon, 18 Mar 1996 13:41
Date: Mon, 18 Mar 1996 13:41
42 lines
1736 bytes
1736 bytes
In article <4ii34r$nm7@solutions.solon.com>,
Kurt Watzka <watzka@stat.uni-muenchen.de> wrote:
>
>timur@vnet.ibm.com (Timur Tabi) writes:
>
>>typedef struct {
>> unsigned uNumLinks;
>> NODE *pnode[1];
>>} NODE_LINK;
>
>>Actually, the second field can be an array of any length, as long as it's at
>>least one element long. In most cases, the array will end up being a few
>>elements long.
>
>In this case, you might run into problems, because the code generated
>by your compiler might have severe problems accessing all but the first
>element of that array. If you want what is conceptually a vector of
>pointers to NODE, you _should_ use
>
> NODE **ppnode;
>
>instead of relying on the "array hack".
How about:
typedef struct {
unsigned uNumLinks;
NODE *pnode[REALLY_LARGE_NUMBER];
} NODE_LINK;
NODE_LINK nl;
nl = malloc(offsetof(NODE_LINK, pnode) + 5 * sizeof(NODE));
/* allocate 5 elements */
This saves 1 malloc call and the code to manage the extra pointer.
[And is technically undefined, because you can't reference a partial object,
but I don't believe anyone's ever found an implementation it fails on. -mod]
sl14@cornell.edu
Witty .sig under construction.
how do I typedef an array of pointers to a structure?
Author: Timur Tabi
Date: Wed, 20 Mar 1996 04:30
Date: Wed, 20 Mar 1996 04:30
34 lines
1900 bytes
1900 bytes
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 This doesn't seem to work right. For starters, I've removed the uNumLinks field, so there are no more counters. I've decided to use a sentinal instead of a count field, because I only rarely need the number of links, and when I do, it's not a real-time requirement. 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? -- Timur "too sexy for my code" Tabi, timur@vnet.ibm.com Architect, OS/2 Warp realtime MIDI subsystem http://timur.austin.ibm.com (internal IBM only) http://www.shirenet.com/~crusader/Timur (external)
Re: how do I typedef an array of pointers to a structure?
Author: Ari Lukumies
Date: Sat, 23 Mar 1996 06:48
Date: Sat, 23 Mar 1996 06:48
44 lines
1913 bytes
1913 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
>
[snip]
> 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?
You could use:
typedef NODE *PNODE; // pointer to a node
typedef PNODE *NODE_SLOTS; // array of ptrs (size indetermined yet)
typedef PNODE_LINKS *PNODE_SLOTS; // likewise, array size yet indetermined
This would equal
typedef NODE ***PNODE_SLOTS;
which is what you're after.
Later,
AriL
--
All my opinions are mine and mine alone.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads