Type casting, c language problem

I can not understand the syntax of the syntax. For instance,

float f=7.0; short s=*(short *)&f; 

What happens here short s=*(short *)&f ? It looks like we throw something as a pointer to a short one and then initialize s to the value stored in the address pointed to by something .

Now this something looks like the address of the variable f . Therefore, if something = address of f , it seems to me that we are doing address of f as a pointer to some short one, and then de-link to it. I know that what I said is wrong, but I just can't imagine it.

Thanks.

+4
source share
4 answers

This syntax would make the most sense if short was the same size as a float , and even then there would be a problem with the " strict alias rules ."

Used to interpret float f bits as representing an integer. It is used to circumvent the fact that s = (short) f; will be interpreted as a conversion to integer. Truncation, I think.

+5
source

Your interpretation is correct. This essentially causes the compiler to process memory f as if it were actually processing short . The result of this will be platform dependent. This is very different from short s = (short)f; which will simply perform a pleasant conversion and is clearly defined.

+2
source

1. When you type something, it should be inside the brackets (). 2. Inside primitive types there is no higher range (size) than other primitive types (outside brackets). 3. After the type distinguishes the variable, it will be stored in the same primitive type that you would convert. 4.Pointer means that it stores the memory address.it will indicate a star in c. (*)

+1
source

It will be:

 float f=7.0; short s=*(short)&f; 

i. In short, a value of 7 is assigned

0
source

All Articles