fsf@kasparov.scs.com (Rick Farnbach) (08/07/90)
How does one create a type, using typedef, that is defined to be a
pointer to a pointer to a pointer... ad infinitum? The straight-forward
approach is:
typedef tree *tree[2]; /* binary tree, for example */
Which is, of course, grossly illegal. What I would like to be able to
do is write lines such as:
tree t;
t[0] = malloc(sizeof(tree));
t[0][1] = malloc(sizeof(tree));
.
.
.
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. Just to pique interest, I add that
PASCAL (blech) *allows* this construct. How can this be done in C?
Thanks,
Rick
NOTE: Please do not waste bandwidth telling the whole network that *you*
can't see any reason for wanting to do this. If you don't have an
answer then keep your (computer) shut!
joefritz@pawl.rpi.edu (Jochen M. Fritz) (08/09/90)
In article <FSF.90Aug6202518@kasparov.scs.com> fsf@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? The straight-forward >approach is: > > typedef tree *tree[2]; /* binary tree, for example */ > this is taken from a project I am working on and it does work. In my application I know how deep the tree can be at most, and if it is not that deep, branch.next.Left == NULL typedef union tree { struct leaf { /*the data that you have goes here */ } leaf; struct branch { /*this builds the linkage field*/ union basetree * Left; union basetree * Right; } branch; } tree; ------------------------------------------------------------------------ | Jochen Fritz | For though we live in the world, we do not | | joefritz@pawl.rpi.edu | wage war as the world does.-- 2 Cor. 10:3 | | usergk2s@rpitsmts.bitnet| You have heard it said, Love your neighbor | | Noah [the peace monger] | and hate your enemy. But I tell you: Love | | | your enemies. Matt. 5:43-44 | ------------------------------------------------------------------------
diamond@tkou02.enet.dec.com (diamond@tkovoa) (08/09/90)
In article <FSF.90Aug6202518@kasparov.scs.com> fsf@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@tkou02.enet.dec.com This is me speaking. If you want to hear the company speak, you need DECtalk.