I want to assign a1 into ana[0], ana1 only inside the storeDataHere function.
Inside saveData function, just need to pass each data_array[0] to storeDataHere function.
#include <stdio.h>
#include <string.h>
struct Analysis{
int a;
int b[10];
};
struct Analysis ana[2];
void storeDataHere(void* res)
{
// initialize a1;
struct Analysis a1;
a1.a = 1;
memset(a1.b, 0, 10*sizeof(int));
for(int i=0; i<10; i++)
a1.b[i] = 1;
// store a1 into ana[0], ana[1];
struct Analysis temp = res;
temp = a1;
}
void saveData(void* data, size_t size)
{
struct Analysis *data_array = data;
// pass two struct elements to storeDataHere;
storeDataHere((void*)data_array[0]);
storeDataHere((void*)data_array[1]);
}
int main()
{
// pass struct array ana to saveData
saveData( (void*)ana, sizeof(ana[0]));
for(int i=0; i<2; i++)
{
printf("\n%d\n", ana[i].a);
for(int j=0; j<10; j++)
printf("%d", ana[i].b[j]);
}
return 0;
}
Here is the output with errors:
How to solve this if I keep this structure of function? (Not to change the function param)

void *to astruct Analysis *in functionsaveData()is likely missing the point of the exercise. One would declare the function parameter as astruct Analysis *in the first place if the function was meant to be specific to that data type.struct Analysisand passing the data around viavoid *. I used search term '[c] struct analysis is:q' and ordered by newest. I'd come across Patrick and mdadurian (and this homework problem) before — and not Jam.