繁体   English   中英

struct 中的动态大小的结构数组

[英]Dynamic sized array of structs in struct

我正在尝试在另一个结构中动态分配结构数组,

typedef struct   {
    float azimuth;      
    float elevation;    
    float radius;       
} SphericalCoords;

typedef struct  {
    SphericalCoords *sphericalCoords;
} Position;

typedef struct  {
    Position positionA;
    Position positionB;
} UpdateVector;

然后在实现中

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;

我正在尝试将coordsAcoordsA分别设置为 SphericalCoords 数组(长度为 3)-但它们从来都不是数组,只是单个实例 SphericalCoords

我在这里做错了什么?

编辑 - 这个问题对我来说是错误的,正在创建数组但我在调试器中看不到它,这是我的误解,我会在调试器中看到它......在代码中我可以访问数组和包含结构和它们的属性[结束问题没有回答]

像这样初始化:

SphericalCoords *coords = malloc(positions * sizeof(SphericalCoords));

现在您可以像这样访问单个成员:

coords[0].azimuth = 0f;
coords[1].azimuth = 0f;
coords[2].azimuth = 0f;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM