
array of pointers to array of structs
Quote:
> Hi everyone!
> my problem is as following:
> i have a dynamically allocated array of structs
> triangle2 huge *tri;
> tri=(triangle2 huge*)farcalloc(numtri,sizeof(triangle2));
There are no huge pointers or farcalloc() in standard C. Those are
extensions from certain compiler. We'll pretend you asked about regular
pointers and allocation functions.
Quote:
> but now i have to create an array of pointers being able to hold
> (in the worst case) all of the triangles for depht sorting them.
> so how do i create an array of (huge) pointers which i can set
> to different triangles? it has to be dynamically allocated just as the
> triangles, as their number varies.
triangle2 **triangles;
triangles = malloc (maxnumtris * sizeof *triangles);
With the standard functions, there is no need to cast the pointer returned,
as it is a void pointer and can be assigned to any object pointer without a
cast.
Now you have a dynamically created array-like object that consists of
pointers, ones that point nowhere and can't be used yet.
Now, when you need to create a specific instance of a triangle you can use
array notation to access it. You must allocate storage for that triangle
before you do anything else with that pointer.
triangles[3] = malloc (sizeof *triangles[3]);
I'm not entirely sure that such a data structure is best for your problem.
I'd think a tree would be better, but without more information on what you
are doing, it's hard to say, and perhaps off topic anyway.
Brian Rodenborn