Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I change a variable of a type to another one in C?

Tags:

c

types

casting

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!

like image 811
drigoSkalWalker Avatar asked May 12 '10 21:05

drigoSkalWalker


People also ask

Can you change variable type in C?

Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.

How do you change a variable data type?

You can change the data type for a variable at any time by using the variable Type setting in the Variables tab. Existing values are converted to the new type. If no conversion is possible, the system-missing value is assigned.

What does change () do in C?

C change pointer address of array in other function to change the position of values in the array.

Can you change variable types in Java?

No, you cannot redeclare variables.


2 Answers

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.

like image 101
Yann Ramin Avatar answered Oct 06 '22 00:10

Yann Ramin


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;


}
like image 40
JaredPar Avatar answered Oct 06 '22 01:10

JaredPar