C++ *((无符号字符*)…)是什么意思?

C++ *((无符号字符*)…)是什么意思?,c++,pointers,C++,Pointers,由于我仍然不能发表评论,我将把这作为一个问题发布。 有人曾经发布过这段代码作为答案(它可以工作,一切都很好),但我真的不知道这段代码是如何工作的。 答案与将字节转换为浮点有关 我知道浮点数是如何工作的,而且我知道他正在使用浮点数的地址,用字符位分配从第一个字节到第四个字节。 我不明白的是这部分代码: *((uchar*) ... ) 我真的很感激你的回答,因为我相信这会让我更好地理解指针和投法 输出(&p),但采用浮点的地址。(uchar*)但表示“将其视为uchar的地址,而不是浮点。然后,


由于我仍然不能发表评论,我将把这作为一个问题发布。
有人曾经发布过这段代码作为答案(它可以工作,一切都很好),但我真的不知道这段代码是如何工作的。

答案与将字节转换为浮点有关

我知道浮点数是如何工作的,而且我知道他正在使用浮点数的地址,用字符位分配从第一个字节到第四个字节。

我不明白的是这部分代码:

*((uchar*) ... )

我真的很感激你的回答,因为我相信这会让我更好地理解指针和投法

输出(&p),但采用浮点的地址。(uchar*)但表示“将其视为uchar的地址,而不是浮点。然后,+x位根据uchar的大小计算地址偏移量,而不是浮点

总之,将其理解为“将此地址视为无符号字符(而不是浮点)的地址,并返回该地址处的字符(上面的uchar数)

您给出的示例代码实际上不是很好的实践——它对浮点的大小做出了某些不可移植的假设


这种类型的强制转换的“最佳实践”是“看看你是否可以避免这样做”,如果不能,至少尝试并确保它不会导致未定义的行为(例如,通过进行无效的内存访问)。

输出(&p>但采用浮点的地址。((uchar*))但表示“将其视为uchar的地址,而不是浮点。然后,+x位根据uchar的大小而不是浮点计算地址偏移量

总之,将其理解为“将此地址视为无符号字符(而不是浮点)的地址,并返回该地址处的字符(上面的uchar数)

您给出的示例代码实际上不是很好的实践——它对浮点的大小做出了某些不可移植的假设


这种类型的强制转换的“最佳实践”是“看看是否可以避免这样做”,如果不能,至少尝试并确保它不会导致未定义的行为(例如,进行无效的内存访问)。

输出(&p>),但会取浮点的地址。(uchar*)但表示“将其视为uchar的地址,而不是浮点。然后,+x位根据uchar的大小计算地址偏移量,而不是浮点

总之,将其理解为“将此地址视为无符号字符(而不是浮点)的地址,并返回该地址处的字符(上面的uchar数)

您给出的示例代码实际上不是很好的实践——它对浮点的大小做出了某些不可移植的假设


这种类型的强制转换的“最佳实践”是“看看你是否可以避免这样做”,如果不能,至少尝试并确保它不会导致未定义的行为(例如,通过进行无效的内存访问)。

输出(&p>但采用浮点的地址。((uchar*))但表示“将其视为uchar的地址,而不是浮点。然后,+x位根据uchar的大小而不是浮点计算地址偏移量

总之,将其理解为“将此地址视为无符号字符(而不是浮点)的地址,并返回该地址处的字符(上面的uchar数)

您给出的示例代码实际上不是很好的实践——它对浮点的大小做出了某些不可移植的假设

这种类型转换的“最佳实践”是“看看你是否可以避免这样做”,如果不能,至少尝试并确保它不会导致未定义的行为(例如,进行无效的内存访问)

此部分将浮点
输出的地址转换为
uchar

*(…)
中的
*
是将结果值分配给uchar
b0
b1
等的解引用运算符

此部分将浮点
输出的地址转换为
uchar

*(…)
中的
*
是将结果值分配给uchar
b0
b1
等的解引用运算符

此部分将浮点
输出的地址转换为
uchar

*(…)
中的
*
是将结果值分配给uchar
b0
b1
等的解引用运算符

此部分将浮点
输出的地址转换为
uchar


*(…)
中的
*
是将结果值分配给uchar
b0
b1
等的解引用运算符。

让我们从头开始:

&output // the address of float variable 'output'

(uchar*)(&output) // take the address of 'output' and treat it 
                  // as it were the address of  unsigned char

(uchar*)(&output) + 3 // add to that address 3*sizeof(unsigned char) bytes
                      // which is 3 bytes

*((uchar*)(&output) + 3) // dereference that address so it becomes
                         // like a 'uchar' variable which lays at the
                         // address which we saw in the previous step

*((uchar*)(&output) + 3) = b0; // assign to it value of 'b0'

让我们从头开始:

&output // the address of float variable 'output'

(uchar*)(&output) // take the address of 'output' and treat it 
                  // as it were the address of  unsigned char

(uchar*)(&output) + 3 // add to that address 3*sizeof(unsigned char) bytes
                      // which is 3 bytes

*((uchar*)(&output) + 3) // dereference that address so it becomes
                         // like a 'uchar' variable which lays at the
                         // address which we saw in the previous step

*((uchar*)(&output) + 3) = b0; // assign to it value of 'b0'

让我们从头开始:

&output // the address of float variable 'output'

(uchar*)(&output) // take the address of 'output' and treat it 
                  // as it were the address of  unsigned char

(uchar*)(&output) + 3 // add to that address 3*sizeof(unsigned char) bytes
                      // which is 3 bytes

*((uchar*)(&output) + 3) // dereference that address so it becomes
                         // like a 'uchar' variable which lays at the
                         // address which we saw in the previous step

*((uchar*)(&output) + 3) = b0; // assign to it value of 'b0'

让我们从头开始:

&output // the address of float variable 'output'

(uchar*)(&output) // take the address of 'output' and treat it 
                  // as it were the address of  unsigned char

(uchar*)(&output) + 3 // add to that address 3*sizeof(unsigned char) bytes
                      // which is 3 bytes

*((uchar*)(&output) + 3) // dereference that address so it becomes
                         // like a 'uchar' variable which lays at the
                         // address which we saw in the previous step

*((uchar*)(&output) + 3) = b0; // assign to it value of 'b0'

uchar
是一个字节。
uchar*
是指向字节的指针。
&output
是浮点
输出的地址
(uchar*)(&output)
输出的地址
转换为字节地址。

因此,
*(uchar*)(&output)
output
,就像是
uchar
一样

uchar
是一个字节。
uchar*
是指向字节的指针。
&output
是浮点
输出的地址
(uchar*)(&output)
输出的地址
转换为字节地址。

因此,
*(uchar*)(&output)
output
,就像是
uchar
一样

uchar
是一个字节。
uchar*
是指向字节的指针。
&output
是浮点
输出的地址
(uchar*)(&output)
输出的地址
转换为字节地址。

因此,
*(uchar*)(&output)
output
,就像是
uchar
一样

uchar
是一个字节。
uchar*
是一个