Re: two dimensional arrays:
- From: Barry Schwarz <schwarzb@xxxxxxxxx>
- Date: Sat, 30 Apr 2005 00:04:38 -0700
On Sat, 30 Apr 2005 00:18:08 +1200, "Hamish" <h.dean@xxxxxxxxxx>
wrote:
>I'm trying to use an C API which is for geometry calculations. The function
>requires an argument for an array of polygons:
>
> coordpt **polygons
>//[0..i..polygons_num-1][0..polygons_vertex_num[i]-1]
The requirement is to use a pointer to pointer to type.
>
>where coordpt is:
>
>typedef struct
> { double x;
> double y;
> } coordpt;
>
>polygons_num is the number of polygons in the calculation,
>and polygons_vertex_num[i] is the number of vertices on polygon i.
>
>Can someone give me an example of how to fill this with valid data. Say the
>data below:
>
>typedef struct
> { coordpt pts[4]; //each polygon up to 4 vertices
> } coordpts;
Here you have an array of type. In some cases, the array can be
treated as a pointer to type.
>
>typedef struct
> { coordpts ptsa[3]; // up to 3 polygons
> } polys;
Now you have built an array of array of type. This can NEVER be
treated as a pointer to pointer to type.
>
>polys * Data;
At some point, Data must be initialized to point to something.
>
snip code that wants to initialize vertices
>
>Right, so I've got this data (actually this will be in a different format,
>with std:vector but that's not important). I want to chuck it into my
There is no stde:vector in C. First you need to decide which language
you will use.
>coordpt **polygons
>
>How do I do this easily?
>
If you want to build P polygons, each with V vertices, your need P
pointers, and each pointer must point to the first of V structs, for a
total of P*V structs.
If P is known at compile time, you can define an array of P pointers
to struct with
coordpt *polygons[P];
If not, you can allocate space for the P pointers during execution
after the value for P has been calculated or input with
coordpt **polygons;
polygons = malloc(P * sizeof *polygons);
If V is known at compile time, you can define an array of V structs
with
coordpt vertices_0[V];
and assign the address of this array (actually the address of the
first element of the array) to a pointer with
polygons[0] = vertices_0;
You would define P of these arrays and assign their addresses to the
pointers.
If V is not known until execution time, you can allocate space for the
vertices with
polygons[0] = malloc(V * sizeof *polygons[0]);
and you would do this P times, usually in a loop.
In any case, you would call the function with
func(polygons);
<<Remove the del for email>>
.
- References:
- two dimensional arrays:
- From: Hamish
- two dimensional arrays:
- Prev by Date: Re: How to get file size?
- Next by Date: re:return a string
- Previous by thread: two dimensional arrays:
- Next by thread: Re: two dimensional arrays:
- Index(es):
Relevant Pages
|