🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#2317
From: Kurt Watzka
Date: Sun, 17 Mar 1996 22:19
67 lines
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
Technical Details
Message-ID: <4ii34r$nm7@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4i804b$46u@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: watzka@stat.uni-muenchen.de (Kurt Watzka)
Newsgroups: comp.lang.c.moderated
Subject: Re: how do I typedef an array of pointers to a structure?
Date: 17 Mar 1996 16:19:07 -0600
Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4ii34r$nm7@solutions.solon.com>
References: <4i804b$46u@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com