Re: struct with indeterminate array(size)
Re: struct with indeterminate array(size)
- Subject: Re: struct with indeterminate array(size)
- From: Greg Titus <email@hidden>
- Date: Thu, 4 Oct 2001 10:57:35 -0700
On Thursday, October 4, 2001, at 09:18 AM, Erik M. Buck wrote:
>
The Objective-C runtime uses the same technique. It is reasonably
>
common in
>
C programs. The trick to make it compile to use a size of 1.
>
>
typedef struct tcPointList
>
{
>
int size;
>
tcPoint points[1]; // this will compile!
>
}tcPointList;
>
Actually, if you use this struct along with the initialization code
mentioned earlier:
>
tcPointList *p = malloc( sizeof( tcPointList ) + sizeof( tcPoint ) *
>
nPoints );
... then you are going to end up allocating an extra tcPoint (the one
actually in the definition of the struct, which adds to the structs
size).
GCC doesn't support the "points[]" construct which started this whole
thread, but it does support "points[0]", which acts the same way. So if
you wanted to keep your C code as close as possible to the same, you
could just use "points[0]" in the struct definition.
On the other hand, if you wanted to use NSMutableArray:
>
Bingo, pretty much had that exact line, but I'm thinking what I really
>
want/need is an NSMutableArray of structs.
>
>
I can make arrays of any normal objects just fine, but if I try for an
>
array of structs I can't access the structs members.
You can do this by turning the structure into an object, but making all
the members public:
@interface TCPoint : NSObject
{
@public
GLfloat x;
GLfloat y;
GLfloat z;
}
@end
You can then add TCPoint objects to your array and treat them as
objects, but still access the members via the -> operator if you want to.
Hope this helps,
--Greg