A "How to typedef..." Question

diamond@tkovoa diamond at tkou02.enet.dec.com
Thu Aug 9 17:57:38 AEST 1990


In article <FSF.90Aug6202518 at kasparov.scs.com> fsf at kasparov.scs.com (Rick Farnbach) writes:

>How does one create a type, using typedef, that is defined to be a
>pointer to a pointer to a pointer... ad infinitum?
>    tree t;
>    t[0] = malloc(sizeof(tree));
>    t[0][1] = malloc(sizeof(tree));

You need a struct.  There is no alternative.

>The closest I have been able to do is:
>    typedef struct _tree {
>        struct _tree *t;
>    } tree[2];
>    tree t;
>    t[0].t = malloc(sizeof(tree));
>    t[0].t[1].t = malloc(sizeof(tree));
>Which is *not* what I am after.

You might try:
    typedef struct _tree {
        struct _tree *t[2];
    } tree;
    tree t;
    t.t[0] = malloc(sizeof(tree));
    t.t[0].t[1] = malloc(sizeof(tree));
I'm not sure if either might be easier than the other, to convert to C++
and get what you want by overloading operator[].

>Just to pique interest, I add that
>PASCAL (blech) *allows* this construct.  How can this be done in C?

In order to get anything useful out of it in Pascal, you'd have to use a
record as well.  You can define  Type x = ^x;  but can't do much with it.
[Incidentally, although Mr. C spelled his name using all capitals, and
requires you to match his casing, Mr. Pascal didn't.]

-- 
Norman Diamond, Nihon DEC     diamond at tkou02.enet.dec.com
This is me speaking.  If you want to hear the company speak, you need DECtalk.



More information about the Comp.lang.c mailing list