hello, I have that strange question about if a function can return a random type of data. Let me explain with a sample
Code:
#include <conio.h>
#include <stdio.h>
#define SPEED 1
#define NAME 2
struct STSPEED {int s,t;};
allDataTypes myStrangeFunction(int what)
{
switch(what)
{
case SPEED:
{
return (allDataTypes)STSPEED;
}
break;
case NAME:
{
return (allDataTypes)char*;
}
break;
}
}
float mySpeed(STSPEED sts)
{
return (float)(sts.s/sts.t);
}
int main()
{
allDataTypes ADT;
/*first let me calcule an auto speed*/
ADT=myStrangeFunction(SPEED);
memset(ADT,0,sizeof(ADT));
ADT.s=100;
ADT.t=1;
printf("%f\n",mySpeed(ADT));
/*and now let me printf my name*/
ADT=myStrangeFunction(NAME);
ADT=(char*)malloc(256);
memset(ADT,0,sizeof(ADT));
sprintf(ADT,"My name");
printf("%s\n",ADT);
free(ADT);
getch();
return 0;
}
obviously that won't compile. my question is if that's possible to create a data type that can be converted to whatever other data type, so on my sample the variable ADT would be fantasiously converted first into a struct and last into a char*.
is it possible to do?
thank's in advance
Niara
PS: I haven't tryed to google for that, nor search on the cboard search engine, because I'm not sure about the title for so ridiculous question