Ada*_*vis 16 c++ printf pointers cout
我从一个简单的cout没有输出,而printf将始终打印数字:
std::cout << variableuint8; // prints nothing
printf("%u", variableuint8); // prints the number
Run Code Online (Sandbox Code Playgroud)
我以前从未遇到过这种行为,虽然我有一个解决方法,但我想了解它.
耶,指针.所以它可能比所述的更多参与.这是上下文 - 我不认为它应该重要,当cout和printf获取信息时,它被解除引用到一个简单的unsigned char.这可能是解决问题所需的更多信息,但我想完全了解它是否相关.
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef struct{
UInt8 len;
// some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;
UInt8 somerandomdata[8];
DeviceNotification * notification;
notification = (DeviceNotification *) somerandomdata;
std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);
Run Code Online (Sandbox Code Playgroud)
随机数据在别处初始化.对于此输出,数组中的第一个字节是0x08:
Len: ()
Len: (8)
Run Code Online (Sandbox Code Playgroud)
ken*_*ytm 12
它正在打印char一个角色.
#include <iostream>
int main() {
unsigned char c = 0x41;
std::cout << c << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会将'A'打印到标准输出.
虽然在这种情况下,您的代码应该打印Len: (*),我确实已经验证它打印Len: (*).
编辑:由于在控制台可能正在使用的ASCII或UTF-8等字符编码中,对应于8(Backspace)的字符不可见,它看起来好像没有打印.
(在某些情况下,它可能会导致前一个字符(the ()消失,因为它是一个退格符.在DOS中它可能会显示◘)
| 归档时间: |
|
| 查看次数: |
12181 次 |
| 最近记录: |