About coercion type conversions (C language)

Source: Internet
Author: User

Because the code you see today uses the coercion type conversion of a struct, it's very important to know what a forced type conversion of a struct is. A struct is as follows: in the following code, RBUF->REQCMDBUF is a null pointer, first assigning the null pointer to a pointer buf of type uint8_t. Then cast the BUF pointer to the Rdma_write_msg_head struct type pointer. So how do we convert it here? First, assign the first 8 bytes of the content pointed to by the BUF pointer to the Rwmh->len variable of the rdma_write_msg_head struct, and then assign the contents of the immediately preceding 1 bytes to Rwmh->flag. As for the contents of the first 9 bytes of the content that the BUF pointer points to, I think it will be discarded. When querying the data for coercion type conversions, you see an example:
Assigns a hexadecimal number to an integer variable, and then attempts to convert the integer to a floating-point type.    1, the first direct to the integer coercion type conversion results are 1112539136.000000. If the address of an integer variable is converted, the result is different. 2, you can see, in the integer variable A to the address of the coercion type conversion, the result is 52.000000.

Summary: Why are the results of Method 1 and Method 2 different? To summarize the reasons, it can be said that: in Method 1 , the integer variable A is directly converted to type A, then the hexadecimal number is treated as an integer, that is, 1112539136,Cast 1112539136 to float, the result is 1112539136.000000. in Method 2 , the address of the integer variable A is converted to a floating-point type, then the memory value in the variable a address is converted to a floating-point type, which is theThe hexadecimal number in the original address value is interpreted as a floating-point format, resulting in a result of 52.000000. PS. In the course of testing the experiment, a simple error was made, which resulted in the following problem: Direct printf ("%f", b) when attempting to output a pointer to a floating-point pointer b; The result is 0.00000.

So modify the code for printf ("%f", *b); 52.000000. But if you keep printf ("%f", b); I'm going to be outputting the result 52.000000 at the beginning I thought it was the Codeblocks compiler did what the cache did to optimize.  However, running this code in the VC + + compiler has different results. In VC + +, there is also printf ("%f", *b), and printf ("%f", b); the output results are 52.00000 and 0.00000, respectively; So this could be a compiler problem.

About coercion type conversions (C language)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.