Convert from struct to float *

I have a structure

typedef struct
{
    float   m[4][4];
} myMatrix;

due to some need in the program i need to convert this to float *

I'm doing something like

if(! g_Fvar16)
    g_Fvar16 = (float*)malloc(sizeof(float) * 16);
memcpy(&g_Fvar16, &struct_var, sizeof(float)*16);
return g_Fvar16;

This is one simple feature. Now, when I call this function, the program crashes when accessing these values. g_Fvar16is anfloat*

sizeof(struct_var) equal to 64, as well as the amount of allocated memory.

Can't I just process the copied memory as float *? I would be the fastest ...

+4
source share
2 answers

g_fVer16 is already a pointer, so you need to write

memcpy(g_Fvar16, &struct_var, sizeof(float)*16);

instead

memcpy(&g_Fvar16, &struct_var, sizeof(float)*16);

(pay attention to the first &)

+4
source

You can just use

float *ptr = &(struct_var.m[0][0]);

"".

, , ( , , struct , ).

+3

All Articles