二维数组: [英] two dimensional arrays:

查看:117
本文介绍了二维数组:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用用于几何计算的C API。函数

需要一个多边形数组的参数:


coordpt **多边形

// [0..i ..polygons_num-1] [0..polygons_vertex_num [i] -1]


其中coordpt是:


typedef struct

{double x;

double y;

} coordpt;


polygons_num是多边形的数量计算,

和polygons_vertex_num [i]是多边形i上的顶点数。


有人能给我一个如何用有效填充它的例子数据。说下面的

数据:


typedef struct

{coordpt pts [4]; //每个多边形最多4个顶点

} coordpts;


typedef struct

{coordpts ptsa [3]; //最多3个多边形

}多边形;


多边形*数据;


数据 - > ptsa [0] .pts [0] .x = 0;

数据 - > ptsa [0] .pts [0] .y = 0;

数据 - > ; ptsa [0] .pts [1] .x = 1;

数据 - > ptsa [0] .pts [1] .x = 0;

数据 - > ptsa [0] .pts [2] .x = 0;

数据 - > ptsa [0] .pts [2] .x = 1; //一个三角形


数据 - > ptsa [1] .pts [0] .x = 0;

数据 - > ptsa [1] .pts [0] .y = 0;

数据 - > ptsa [1] .pts [1] .x = 1;

数据 - > ptsa [ 1] .pts [1] .x = 0;

数据 - > ptsa [1] .pts [2] .x = 1;

数据 - > ptsa [1] .pts [2] .x = 1;

数据 - > ptsa [1] .pts [3] .x = 0;

数据 - > ptsa [1] .pts [3] .x = 1; //一个正方形


数据 - > ptsa [2] .pts [0] .x = 0;

数据 - > ptsa [2] .pts [0] .y = 0;

数据 - > ptsa [2] .pts [1] .x = 2;

数据 - > ptsa [ 2] .pts [1] .x = 0;

数据 - > ptsa [2] .pts [2] .x = 2;

数据 - > ptsa [2] .pts [2] .x = 2;

数据 - > ptsa [2] .pts [3] .x = 0;

数据 - > ptsa [2] .pts [3] .x = 2; //一个更大的正方形

polygons_num = 3;

int polygons_vertex_num [3]; //最多3个多边形的顶点

polygons_vertex_num [0] = 3;

polygons_vertex_num [1] = 4;

polygons_vertex_num [2] = 4;


对,所以我有这些数据(实际上这将采用不同的格式,

with std:vector但是'不重要)。我想把它扔进我的

coordpt **多边形


我如何轻松地做到这一点?

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]

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;

typedef struct
{ coordpts ptsa[3]; // up to 3 polygons
} polys;

polys * Data;

Data->ptsa[0].pts[0].x = 0;
Data->ptsa[0].pts[0].y = 0;
Data->ptsa[0].pts[1].x = 1;
Data->ptsa[0].pts[1].x = 0;
Data->ptsa[0].pts[2].x = 0;
Data->ptsa[0].pts[2].x = 1; // a triangle

Data->ptsa[1].pts[0].x = 0;
Data->ptsa[1].pts[0].y = 0;
Data->ptsa[1].pts[1].x = 1;
Data->ptsa[1].pts[1].x = 0;
Data->ptsa[1].pts[2].x = 1;
Data->ptsa[1].pts[2].x = 1;
Data->ptsa[1].pts[3].x = 0;
Data->ptsa[1].pts[3].x = 1; //a square

Data->ptsa[2].pts[0].x = 0;
Data->ptsa[2].pts[0].y = 0;
Data->ptsa[2].pts[1].x = 2;
Data->ptsa[2].pts[1].x = 0;
Data->ptsa[2].pts[2].x = 2;
Data->ptsa[2].pts[2].x = 2;
Data->ptsa[2].pts[3].x = 0;
Data->ptsa[2].pts[3].x = 2; //a bigger square

polygons_num = 3;
int polygons_vertex_num[3]; //vertices for up to 3 polygons
polygons_vertex_num[0] = 3;
polygons_vertex_num[1] = 4;
polygons_vertex_num[2] = 4;

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
coordpt **polygons

How do I do this easily?

推荐答案

2005年4月30日星期六00:18:08 +1200,Hamish < h。**** @ xtra.co.nz>

写道:
On Sat, 30 Apr 2005 00:18:08 +1200, "Hamish" <h.****@xtra.co.nz>
wrote:
我正在尝试使用的C API是几何计算。函数
需要一个多边形数组的参数:

coordpt **多边形
// [0..i..polygons_num-1] [0..polygons_vertex_num [ i] -1]


要求是使用指向类型的指针。
其中coordpt是:

typedef struct
{double x;
double y;
} coordpt;

polygons_num是计算中多边形的数量,
和polygons_vertex_num [i]是数字多边形上的顶点i。

有人能给我一个如何用有效数据填充它的例子。说下面的
数据:

typedef struct
{coordpt pts [4]; //每个多边形最多4个顶点
} coordpts;


这里有一个类型的数组。在某些情况下,数组可以被视为指向类型的指针。
typedef struct
{coordpts ptsa [3]; //最多3个多边形
} polys;


现在你已经构建了一个类型数组的数组。这绝不可能被视为指向类型指针的指针。

polys * Data;


在某些时候,必须初始化数据以指向某个东西。


想要初始化顶点的剪辑代码
对,所以我有这些数据(实际上这将是一个不同的格式,
与std:vector,但这并不重要)。我想把它扔进我的


没有stde:C中的向量。首先你需要决定使用哪种语言



coordpt **多边形

如何轻松完成这项工作?
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?



如果你想构建P多边形,每个都有V个顶点,你的需要P

指针,并且每个指针必须指向V个结构的第一个,总计P * V结构的




如果在编译时已知P,则可以定义一个P指针数组

来构造

coordpt *多边形[P];


如果没有,你可以在执行期间为P指针分配空间

在计算P值或输入后使用

coordpt **多边形;

多边形= malloc(P * sizeof *多边形);


如果在编译时已知V,则可以定义V个结构的数组

with

coordpt vertices_0 [V];

并指定此数组的地址(实际上是地址o f /

数组的第一个元素)指向

多边形[0] = vertices_0;

您将定义这些数组的P并将其地址分配给

指针。


如果在执行时间之前不知道V,则可以为
$ b $分配空间b顶点

多边形[0] = malloc(V * sizeof *多边形[0]);

你会这样做P次,通常是循环。


在任何情况下,你都可以用

func(多边形)调用函数;

<<删除del电子邮件>>


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




Hamish写道:

Hamish wrote:
我正在尝试使用的是一个C API用于几何计算。
函数需要一个多边形数组的参数:

coordpt **多边形
// [0..i..polygons_num-1] [0..polygons_vertex_num [ i] -1]

其中coordpt是:

typedef struct
{double x;
double y;
} coordpt;

polygons_num是计算中多边形的数量,
和polygons_vertex_num [i]是多边形i上的顶点数。

有人能给我一个例子吗?如何用有效数据填充它。
说下面的数据:

typedef struct
{coordpt pts [4]; //每个多边形最多4个顶点
} coordpts;

typedef struct
{coordpts ptsa [3]; //最多3个多边形
}多边形;

多边形*数据;

数据 - > ptsa [0] .pts [0] .x = 0 ;
数据 - > ptsa [0] .pts [0] .y = 0;
数据 - > ptsa [0] .pts [1] .x = 1;
数据 - > ptsa [0] .pts [1] .x = 0;
数据 - > ptsa [0] .pts [2] .x = 0;
数据 - > ptsa [0 ] .pts [2] .x = 1; //一个三角形

数据 - > ptsa [1] .pts [0] .x = 0;
数据 - > ptsa [1] .pts [0] .y = 0;
数据 - > ptsa [1] .pts [1] .x = 1;
数据 - > ptsa [1] .pts [1] .x = 0;
数据 - > ptsa [1] .pts [2] .x = 1;
数据 - > ptsa [1] .pts [2] .x = 1;
数据 - > ptsa [ 1] .pts [3] .x = 0;
数据 - > ptsa [1] .pts [3] .x = 1; //一个正方形

数据 - > ptsa [2] .pts [0] .x = 0;
数据 - > ptsa [2] .pts [0] .y = 0;
数据 - > ptsa [2] .pts [1] .x = 2;
数据 - > ptsa [2] .pts [1] .x = 0;
数据 - > ptsa [2] .pts [2] .x = 2;
数据 - > ptsa [2] .pts [2] .x = 2;
数据 - > ptsa [ 2] .pts [3] .x = 0;
数据 - > ptsa [2] .pts [3] .x = 2; //一个更大的正方形

polygons_num = 3;
int polygons_vertex_num [3]; //最多3个多边形的顶点
polygons_vertex_num [0] = 3;
polygons_vertex_num [1] = 4;
polygons_vertex_num [2] = 4;

右,所以我有这些数据(实际上这将是一个不同的
格式,std:vector,但这并不重要)。我想把它放进我的
coordpt **多边形

我该如何轻松地做到这一点?
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]

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;

typedef struct
{ coordpts ptsa[3]; // up to 3 polygons
} polys;

polys * Data;

Data->ptsa[0].pts[0].x = 0;
Data->ptsa[0].pts[0].y = 0;
Data->ptsa[0].pts[1].x = 1;
Data->ptsa[0].pts[1].x = 0;
Data->ptsa[0].pts[2].x = 0;
Data->ptsa[0].pts[2].x = 1; // a triangle

Data->ptsa[1].pts[0].x = 0;
Data->ptsa[1].pts[0].y = 0;
Data->ptsa[1].pts[1].x = 1;
Data->ptsa[1].pts[1].x = 0;
Data->ptsa[1].pts[2].x = 1;
Data->ptsa[1].pts[2].x = 1;
Data->ptsa[1].pts[3].x = 0;
Data->ptsa[1].pts[3].x = 1; //a square

Data->ptsa[2].pts[0].x = 0;
Data->ptsa[2].pts[0].y = 0;
Data->ptsa[2].pts[1].x = 2;
Data->ptsa[2].pts[1].x = 0;
Data->ptsa[2].pts[2].x = 2;
Data->ptsa[2].pts[2].x = 2;
Data->ptsa[2].pts[3].x = 0;
Data->ptsa[2].pts[3].x = 2; //a bigger square

polygons_num = 3;
int polygons_vertex_num[3]; //vertices for up to 3 polygons
polygons_vertex_num[0] = 3;
polygons_vertex_num[1] = 4;
polygons_vertex_num[2] = 4;

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
coordpt **polygons

How do I do this easily?




查看以下内容用于构建二维数组的代码:
http://code.axter。 com / allocate2darray.h
http://code.axter .com / allocate2darray.c


使用上面的代码,您可以通过以下

方法创建一个二维数组:

int x = 4;

int y = 6;


coordpt ** My_coordpt = ALLOCATE2DARRAY(coordpt,x,y);



Check out the following code for building a 2 dimensional array:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c

Using above code, you can create a 2 dimensional array via following
method:
int x = 4;
int y = 6;

coordpt **My_coordpt = ALLOCATE2DARRAY(coordpt, x, y);


2005年4月30日04:02:54 -0700,Axter < TE ** @ axter.com>写道:
On 30 Apr 2005 04:02:54 -0700, "Axter" <te**@axter.com> wrote:

Hamish写道:

Hamish wrote:
我正在尝试使用用于几何计算的C API。函数
I''m trying to use an C API which is for geometry calculations. Thefunction
需要多边形数组的参数:

coordpt **多边形
// [0..i..polygons_num-1] [0。 .polygons_vertex_num [i] -1]

其中coordpt是:

typedef struct
{double x;
double y;
} coordpt;
requires an argument for an array of polygons:

coordpt **polygons
//[0..i..polygons_num-1][0..polygons_vertex_num[i]-1]

where coordpt is:

typedef struct
{ double x;
double y;
} coordpt;



snip
查看以下用于构建二维数组的代码:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c

使用上面的代码,你可以通过以下方法创建二维数组:
int x = 4;
int y = 6;

coordpt ** My_coordpt = ALLOCATE2DARRAY(coordpt,x ,y);


snip
Check out the following code for building a 2 dimensional array:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c

Using above code, you can create a 2 dimensional array via following
method:
int x = 4;
int y = 6;

coordpt **My_coordpt = ALLOCATE2DARRAY(coordpt, x, y);




您的宏转换为对返回void的例程的调用**和

然后将该值转换为所需的类型。虽然这在大多数系统上可能都有效,但无法保证无效**以及与coordpt **兼容的
或者通过演员表转换

产生一个有意义的值。


此外,例程分配一个void *块并返回该块的

地址。同样,虽然这可能适用于大多数

系统,但不能保证sizeof(void *)与

sizeof(coordpt *)或者表示相同两个

指针类型中的地址是相同的。


由于你的两个分配函数使用相同的分配逻辑,为什么

是否使用void **和另一个unsigned char **?


在第二个函数中,你有一个无用的(并且不正确但是

无害)将第二个参数转换为memcpy。暂时忽略memcpy'原型中的const

,memcpy需要一个void *。

函数接收void *作为其参数。你为什么把它转换成

unsigned char *只是因为编译器必须将它转换回

你呢?


您的分配函数都没有检查malloc的返回值

成功。

<<删除电子邮件的del>>



Your macro translates as a call to a routine that returns a void** and
then casts that value to the desired type. While this probably works
on most systems, there is no guarantee that a void** is in any way
compatible with coordpt** or that the conversion via the cast will
produce a meaningful value.

Furthermore, the routine allocates a block of void* and returns the
address of this block. Again, while this probably works on most
systems, there is no guarantee that sizeof(void*) is the same as
sizeof(coordpt*) or that the representation of an address in the two
pointer types is the same.

Since your two allocate functions use the same allocation logic, why
does one use void** and the other unsigned char**?

In the second function, you have a useless (and incorrect but
harmless) cast of the second argument to memcpy. Ignoring the const
in memcpy''s prototype for a moment, memcpy expects a void*. The
function receives a void* as its argument. Why do you cast it to
unsigned char* just so the compiler will have to convert it back for
you?

Neither of your allocate functions checks the return from malloc for
success.
<<Remove the del for email>>


这篇关于二维数组:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆