Listing a dataset for a compatible structure

I am in a situation where my code receives data from somewhere outside my control in the form of a long list floats.

These numbers are distributed among various functions.

void myfunc(struct floatstruct* fs);

which take structures of the following form:

struct floatstruct
{
    float a;
    float b;
    float c;
};

You get the idea.

I was wondering if there is a way to safely pass a float array in floatstructto transfer data directly to myfunc. I can add alignment attributes to floatstructif necessary.

An example of the desired behavior:

struct mystruct1
{
    float a;
    float b;
    float c;
};

struct mystruct2
{
    float x;
    float y;
};

extern void myfunc1(mystruct1*);
extern void myfunc2(mystruct2*);

void process_data(float* numbers)
{
    myfunc1((struct mystruct1*)numbers);
    myfunc2((struct mystruct2*)(numbers + 3));
}

The ideal solution, of course, should change the system. But I am looking for solutions according to the given parameters.

+4
source share
2 answers

, : ( , 3 , , ...)

:

    struct floatstruct
{
    float (*a[3]);
};



{
    int i;
    struct floatstruct aStruct;
    struct floatstruct bStruct;
    float *num = numbers;
    for (i = 0; i < 6; i++) {
        if (i < 3)
            aStruct.a[i] = num;
        else 
            bStruct.a[i-3] = num;
        num++;
    }

    myfunc1(&aStruct);
    myfunc2(&bStruct);
}
0

, :

#pragma pack(sizeof(float))
struct mystruct1
{
    float a;
    float b;
    float c;
};

struct mystruct2
{
    float x;
    float y;
};
#pragma pack()

typedef union
{
   mystruct1 struct1;
   mystruct2 struct2;
}structsUnion;

void myfunc1(structsUnion* values)
{
    values->struct1.a; // to access members
}
void myfunc2(structsUnion* values)
{
    values->struct2.x; // to access members
}

void process_data(float* numbers)
{
    myfunc1((structsUnion*)(numbers));
    myfunc2((structsUnion*)(numbers));
}
-1

All Articles