I want to cast an object in C to another type without converting the data and without need to use pointers. I know that you can do it in this manner:
float a = 1.2;
int b = *(int*)(&a);
But I would like to be able to do it to literals and structs constructed in place (if thats the right terminology for that). For example (imagine I could reference a literal for a moment):
void somefunc(int i){...}
somefunc(*(int*)(&1.2))
What I mean by constructed in place is, say I have a struct vec2:
struct vec2{
float x,y;
};
I would like to be able to do something like
void somefunc(vec2 v){...}
somefunc(ConversionlessCast(vec2{1.f,2.f}))
Is this possible?
Edit: I'd like to clarify that I do not care if this is safe or not. I just want to know if theres a way to do it. I should clear up that I would like to not have to make staging variables as well.
Edit2: I realize how poorly formulated this question is without an example, and while initially typing out the example I realized what the answer to what I want to use this for is anyways.
I'm playing around with a way to do styling for a ui system I'm developing and want to be able to use one function for setting a style property. I wanted to pass a string then a value and use the string to determine how to interpret the value. It's now obvious that I should use va_args to do so. However I'm still interested in if there's a way to do what I originally asked regardless because my original plan was to use a unioned type that holds all types a property can possibly be, like
struct Property{
union{
u32 val_u32;
f32 val_f32;
//other possible types
};
};
and then use a string that precedes this to choose which value to use. Unfortunately using an initializer list only accepts the first type, a u32. SO I wanted to come up with a way to initialize a property in a manner like
#define Property(x) Property{(magic that reinterprets anything as the first type on property})
Property(vec2{1.f,2.f})