7

I want to do it:

int main () {
  bla bla bla

  void *onetype;

  switch (USER_INPUT_TYPE) {

      CASE CONVERT_TO_CHAR:
          convert onetype VOID TO CHAR >>> HOW???

      CASE CONVERT_TO_INT:
          convert onetype VOID TO INT >>> HOW???

   LOT OF CASES...
   }
}

Yes, I know type casting, but type casting is a 'temporary' change.

So, is there any way to accomplish it in C?

EDIT :

Stop stop stop! Please, see, what are you doing is type casting, I KNOW THIS, you are creating another variable of the desirable type like int i = (int) onetype, I don't want this, I want something else like onetype = (int) onetype, without recreate them, without allocate another variable.

Thanks a lot guys!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
drigoSkalWalker
  • 2,742
  • 9
  • 32
  • 45
  • 2
    If you don't like the answers given, it sounds like what you really want can't be done. – WhirlWind May 12 '10 at 21:21
  • I liked the answers, but it is not the answer that i wanted...I only put them in the correct way...sorry if somebody feels injured. – drigoSkalWalker May 12 '10 at 21:25
  • You said "without allcat[ing] another variable". Most compilers with optimizations turned on would not use more memory or registers to create a new variable of the desired type to convert the old variable (of the undesired type) to as long as you did not need to keep the old variable around as well. The compiler would consider the old variable dead and reuse its memory or register (as long as it isn't static, global, or referenced by a pointer). If you've ever used a debugger and try to check the value of a local variable but it says you can't this is probably the reason. – nategoose May 13 '10 at 00:00

3 Answers3

7

What you want is run-time type information - to have a variable in which the type is only determinable at run time. C does NOT have this functionality in the language - once the program is compiled, types are erased, and only memory blobs exist. Dynamic languages maintain type information and implement this natively.

You can devise your own home-grown type tagging system:

typedef union {
int i;
char c;
float f;
} evil;

typedef struct {
  evil value;
  int type;
} tagged_t;

enum {
  TYPE_INT, TYPE_CHAR, TYPE_FLOAT
};

tagged_t bar;
bar.value.c = 'a';
bar.type = TYPE_CHAR;

Now every time you wish to use your tagged_t type, you must implement a condition for each possible type of variable you are storing, or be able to determine whether a type is allowed in that area of code or not.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • thanks a lot, but, why you named your union as evil? Is there any risks to use union? thanks a lot again. – drigoSkalWalker May 12 '10 at 21:29
  • 3
    There is nothing inherently evil about unions. The only evil exists in the added complexity of knowing how to interpret the union. – Yann Ramin May 12 '10 at 21:31
1

It sounds like your scenario is as follows

  • void* onetype holds a pointer to a strongly typed variable
  • USER_INPUT_TYPE tells you the type of that variable

If that's the case then try the following

switch (USER_INPUT_TYPE) {
case CONVERT_TO_CHAR:
  char c = *((char*)onetype);
  ...
  break;
case CONVERT_TO_INT:
  int i = *((int*)onetype);
  ...
  break;


}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0
int lolcakes = *(void*)onetype;

This is assuming that you know for sure that onetype points to an int. Using this type of casting is incredibly unsafe.

Puppy
  • 144,682
  • 38
  • 256
  • 465