array of pointers to array of structs 
Author Message
 array of pointers to array of structs

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));

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.

thanks in advance
Florian



Mon, 16 May 2005 23:00:32 GMT  
 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



Mon, 16 May 2005 23:43:22 GMT  
 array of pointers to array of structs
On 28 Nov 2002 16:00:32 +0100, "Florian H?nel" <heeen-at-gmx.de>

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));

>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.

>thanks in advance
>Florian

You have allocated space for numtri objects of type triangle2.

You now say you want to allocate space for numtri objects of type
trianlge2*.

The process is exactly the same.  To dynamically allocate an array of
type X, you declare a pointer to type X and allocate space for (x)
objects of type X.  In this case, X is pointer to triangl2.  Not
knowing anything about huge or far, I suggest you try something along
the lines of

        triangle2 **tri_p;
        tri_p = calloc(numtri, sizeof *tri_p);
        for (i = 0; i < numtri; i++)
                tri_p[i] = &tri[i];

<<Remove the del for email>>



Tue, 17 May 2005 06:29:37 GMT  
 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));

I hope you have #defined 'huge' and 'farcalloc' somewhere, because they have
no meaning in C.  On the assumption that you have correctly typedefed
triangle2 and declared and assigned numtri somewhere, rewrite this as (after
#including <stdlib.h>):

triangle2 *tri;
tri = calloc(numtri, sizeof *tri);

And be sure to check that the call succeeded.



Tue, 17 May 2005 13:15:58 GMT  
 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));

> I hope you have #defined 'huge' and 'farcalloc' somewhere, because
> they have no meaning in C.  On the assumption that you have correctly
> typedefed triangle2 and declared and assigned numtri somewhere,
> rewrite this as (after #including <stdlib.h>):

> triangle2 *tri;
> tri = calloc(numtri, sizeof *tri);

> And be sure to check that the call succeeded.

thanks all, i'll check your suggestions.
huge and farcalloc are (ms)dos specific modifiers to access
arrays greater than 64kb and are predefined by borland turboc++

Florian



Tue, 17 May 2005 20:27:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. qsorting & managing struct arrays, pointer arrays

2. struct.array works - struct->pointer not

3. Array of pointers, pointer to array...

4. array pointer/pointer array

5. arrays pointers array of pointers

6. Referencing an array of structs in an array of stracts

7. problem realloc array inside array of struct

8. problem realloc array inside array of struct

9. VC7.1 - Error C2059 - pointer to array of structs

10. Array of struct Pointers

11. Help: Pointer to 2-D array of structs.

12. array of struct pointers?

 

 
Powered by phpBB® Forum Software