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.

thanks

What I have tried:

I tried No Success:

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

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

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


Could you please state clearly what are your requirements?
   
Comments
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
   
5ed!
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:
C++
struct 
   {
   int n; 
   float f;
   } dummy;
Or often better
C++
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:
C++
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:
C++
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:
C++
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.
   
Comments
Maciej Los 19-Jun-20 5:30am
   
5ed!

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