0

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

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]

garrilla
  • 539
  • 5
  • 15
  • 1
    `SphericalCoords* coordsA ; coordsA = (SphericalCoords*)realloc(coordsA,(positions * sizeof(SphericalCoords))); SphericalCoords* coordsB;` invalid: calling `realloc()` with uniitialized `coordsA`. Try `SphericalCoords* coordsA = NULL;`. – chux - Reinstate Monica Jun 08 '20 at 14:19
  • The first argument to `realloc()` needs to be a pointer that was returned by malloc/realloc or `NULL`. You're using an uninitialized pointer, causing undefined behavior. – Barmar Jun 08 '20 at 14:44
  • [dont cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Jun 08 '20 at 14:45
  • Except from the `realloc` problem showed by chux and Barmar, why do you say that `coordsA` is not an array? – Serge Ballesta Jun 08 '20 at 14:49
  • adding a malloc() to the initialisation doesn't change anything - when I check in the debugger, `coordsA` is just a single instance of SphericalCoords – garrilla Jun 08 '20 at 14:54

1 Answers1

0

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;
jafarlihi
  • 50
  • 1
  • 4
  • 15
  • that does not result in `coords` being an array – garrilla Jun 08 '20 at 15:16
  • @garrilla It does. In C, arrays and pointers are not much different. `coords[1]` is basically the same thing as `*(coords + 1)`. Since you have malloc'd 3 times the size of the pointer type `SphericalCoords` you can safely access `coords[0]`, `coords[1]`, and `coords[2]`. Pointer arithmetic is such that adding 1 to a pointer is same as iterating forward 1 times the size of pointer type. – jafarlihi Jun 08 '20 at 15:24
  • OK, maybe I can be clearer. When I evaluate this code in the debugger, `coords` is not an array. – garrilla Jun 08 '20 at 15:25
  • @garrilla: To the debugger, `coords` is not an array, because it is a pointer. It points to a `SphericalCoords`. That `SphericalCoords` is the first of three `SphericalCoords` that you allocated. So there is an array of three `SphereicalCoords` there, and `coords` points to the first of them. Due to the way C expressions and operators work, you can access elements of that array with `coords[i]`. There is an array there, the pointer points to the first of them, and the debugger does not know about the array. – Eric Postpischil Jun 08 '20 at 16:39
  • @ericpostpischil thanks, your explanation helped me understand – garrilla Jun 09 '20 at 14:59