Click here to Skip to main content
15,070,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to cast by converting "variable as type"

struct {int n; float f;} dummy;
float number=(dummy.f)123.456;

also if we know that "dummy.f" is A "placeholder" structure record address %0x04 in x86 Platform Typed "float", so if everything about the "variable" is known, it must be straightforward for simple easy type casting.

isn't it ?

I think the bug is in the base core of C language.


What I have tried:

I tried No Success:

float number=*(dummy.f*)123.456;
float number=*(dummy.theFloatAddress*)123.456;
Updated 18-Jun-20 21:49pm

You cannot do that, dummy.f is not a type, is a member of a struct, having type float.
You can simply write
float number = 123.456;

or, depending on your needs
float number = dummy.f;

Could you please state clearly what are your requirements?
Member 11010948 19-Jun-20 3:50am
sorry. you missed the point of casting!
CPallini 19-Jun-20 4:11am
And what is the 'point of casting'?
jeron1 19-Jun-20 10:48am
To catch fish of course! ;-)
Maciej Los 19-Jun-20 5:29am
BTW: OP (probably) wants to set value to the member of structure. See OriginalGriff's answer.
CPallini 19-Jun-20 6:34am
Thank you!
A struct is a type that you construct to hold related values together:
   int n; 
   float f;
   } dummy;
Or often better
typedef struct 
   int n; 
   float f;
   } dummy;
So that when you create an instance of the struct, it contains both the integer and floating point values you need together (instead of creating individual variables for them each time you need them both). This is particularly useful when you want to keep collections of items "together" - instead of declaring two arrays:
int ns[100];
int fs[100];
and having to remember to update the right indexes for both you can declare an array of the struct:
dummy arrayOfReltedValues[100];
and teh sytem will "keep" related values together.

So when you want to access the values it's pretty simple, you just use dot syntax:
dummy dum;
dum.n = 666;
dum.f = 1.2345;
printf("%u:%f\n", dum.n, dum.f);
And you don't need to cast anything because the system "knows" what type each part of the struct is.
Maciej Los 19-Jun-20 5:30am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900