typedefing arrays

Steve Lamont spl at mcnc.org
Tue Jun 6 00:00:59 AEST 1989


Gentlefolk:

  The recent discussions about aggregate initializations and so forth
got me to thinking (this is always a dangerous thing) about typedefed
aggregates.  I know that you can typedef something like

	typedef struct {
		float	x;
		float	y;
		float	z;
	} vertex;

and then treat vertex almost in the same way as a primitive type (such
as char, int, float, etc).  F'rinstance, you can allocate an array of
vertex in the following manner

	vertex 	*v;

	v = ( vertex *) malloc( sizeof( vertex ) * some_number );

and do wonderful things with v.

  Now to the nub of my question:

  Is it possible to do something like

	typedef int	vertex[3];

	vertex	*v;

	v = ( vertex *) malloc( sizeof( vertex ) * some_number );

My compiler will swollow it, and, if I play all sorts of funny games
with pointer dereferencing, I can even put numbers into the array v and
pull them out again in some semblance of correct order.  However, all of
the solutions I come up with are inelegant, at best. 

  So, to the question before the house.  Is this a sensible thing to do?
Is it even valid C (as I say, I can get both Microsoft 5.1 and BSD 4.3 C
compilers to swollow it)?  How do I reference individual elements
(v[0], v[1], v[2]) as I am able to in the first instance (v->x, v->y,
v->z)?  Do I have to resort to klugey constructs to use this construction
profitably?  Or should I just forget the whole thing?

-- 
							spl
Steve Lamont, sciViGuy			EMail:	spl at ncsc.org
North Carolina Supercomputing Center	Phone: (919) 248-1120
Box 12732/RTP, NC 27709



More information about the Comp.lang.c mailing list