Reputation: 25
I am trying to create an automated template in embedded C, which has a fixed plane text format, interspersed with variable parameters which I need to pick up from say an array. So I am putting in place holders at the point of occurrence of the parameters. Some of the parameters are strings while others are integer values or floating point values.
I have inserted extended ASCII values as place holders and then subtract 0x80 to extract an index based on the point of occurrence of the place holder. But I don't have a way to point to different source tables yet.
Upvotes: 0
Views: 76
Reputation: 2547
By definition Array is collection of similar data types. So there is no no straight forward way in which you can have a array of pointers to different data types.
One of the way by which you can achieve this is by type casting
the array elements with intended data types while accessing/ de-referencing the elements.
To know the correct type
while accessing the elements you need to store the type while defining the element.
A possible solution could look like this:
typedef struct
{
int type; //0 - enumDay, 1 - Struct C etc
void* data;
}mydata_t;
mydata_t myarray[10];
Assigning :
myarray[0].type = 2; //Lets assume 2 for string
myarray[0].data = "My String";
Using :
if( myarray[0].type == 2) // is it a string?
{
printf("This is the string :%s \n",(char*)myarray[0].Data);
}
Upvotes: 2