开发者

Creating a unknown data type array into a structure?

开发者 https://www.devze.com 2023-04-06 10:09 出处:网络
I have a structure that I used to populate Class Structures with values: 开发者_开发百科MyType structMy[] =

I have a structure that I used to populate Class Structures with values:

开发者_开发百科
MyType structMy[] =
{
  { START,  INTEGER_TYPE, 3, (void *)&classStart->statusStart.set    },
  { STABLE, CHAR_TYPE,    5, (void *)&classtStable->statusStable.set },
  { STOP,   DOUBLE_TYPE,  1, (void *)&classStop->statusStop.set      }
}

But for testing and validation I want to add test cases to the structure: some values which depend on the defined data type per line and number of values.

But because of the structure setup and 1 value or an array, I think I need a (void*). But the compiler doesn't like it. What can I do to write an array into a structure where data types can change?

MyType structMy[] =
{
  { START,  INTEGER_TYPE, 3,  (void*){0, 1, 2}                 },
  { STABLE, CHAR_TYPE,    5,  (void*){'A', 'B', 'C', 'D', 'E'} },
  { STOP,   DOUBLE_TYPE,  1,  (void*){2.4}                     }
}


The compiler wants pointers there, so try declaring the data elsewhere:

int is[] = {0, 1, 2};
char cs[] = {'A', 'B', 'C', 'D', 'E'};
double ds[] = {2.4};

MyType structMy[] =
{
     {START, INTEGER_TYPE, 3,  (void*)is },
     {STABLE, CHAR_TYPE, 5,  (void*)cs },
     {STOP, DOUBLE_TYPE, 1,  (void*)ds }
}


By telling the compiler what type to expect, it can be done like this:

MyType structMy[] =
{
   { START,  INTEGER_TYPE, 3,  (void*)(int[]){0, 1, 2}                  },
   { STABLE, CHAR_TYPE,    5,  (void*)(char[]){'A', 'B', 'C', 'D', 'E'} },
   { STOP,   DOUBLE_TYPE,  1,  (void*)(double[]){2.4}                   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消