[英]Dynamic sized array of structs in struct
I'm trying to dynamically assign an array of structs in another struct,我正在尝试在另一个结构中动态分配结构数组,
typedef struct {
float azimuth;
float elevation;
float radius;
} SphericalCoords;
typedef struct {
SphericalCoords *sphericalCoords;
} Position;
typedef struct {
Position positionA;
Position positionB;
} UpdateVector;
then in the implementation然后在实现中
int postions= 3;
UpdateVector *uv;
uv = malloc(sizeof(UpdateVector));
SphericalCoords* coordsA ;
coordsA = (SphericalCoords*)realloc(coordsA,(positions * sizeof(SphericalCoords)));
SphericalCoords* coordsB;
coordsB = (SphericalCoords*)realloc(coordsB,(positions * sizeof(SphericalCoords)));
if(coordsA)
uv->positionA.sphericalCoords = coordsA;
if(coordsB)
uv->positionB.sphericalCoords = coordsB;
I'm trying to set coordsA
& coordsA
to each be an array of SphericalCoords (with length 3) - but they're never an array, just a single instance SphericalCoords我正在尝试将
coordsA
和coordsA
分别设置为 SphericalCoords 数组(长度为 3)-但它们从来都不是数组,只是单个实例 SphericalCoords
What am I doing wrong here?我在这里做错了什么?
EDIT - this question was wrong on my part, the array was being created but I could not see it in the debugger, and that was my misunderstanding that I would see it in the debugger... in code I could access the array and the contained structs and their properties [closing question without answering]编辑 - 这个问题对我来说是错误的,正在创建数组但我在调试器中看不到它,这是我的误解,我会在调试器中看到它......在代码中我可以访问数组和包含结构和它们的属性[结束问题没有回答]
Initialize like this:像这样初始化:
SphericalCoords *coords = malloc(positions * sizeof(SphericalCoords));
Now you can access individual members like this:现在您可以像这样访问单个成员:
coords[0].azimuth = 0f;
coords[1].azimuth = 0f;
coords[2].azimuth = 0f;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.