Siv*_*nan 8 c scanf format-specifiers
使用hhx而不是x有什么用,在下面的代码中,mac_str是char指针而mac是uint8_t数组,
sscanf(mac_str,"%x:%x:%x:%x:%x:%x",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
Run Code Online (Sandbox Code Playgroud)
当我尝试上面的代码时它发出警告,
warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 8 has type ‘uint8_t *’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
但我在他们指定的一些代码中看到了
sscanf(str,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
Run Code Online (Sandbox Code Playgroud)
哪个不给任何警告
但两者都是一样的,使用hhx而不是x的需要是什么,我在网上搜索但没有直接回答
&mac[0]是一个指针unsigned char.1 %hhx表示相应的参数指向a unsigned char.使用方形钉用于方孔:格式字符串中的转换说明符必须与参数类型匹配.
1实际上,&mac[0]是指向a的指针uint8_t,%hhx但仍然是错误的uint8_t.它在许多实现中"起作用",因为uint8_t它与许多实现中的相同unsigned char.但正确的格式是"%" SCNx8,如:
#include <inttypes.h>
…
scanf(mac_str, "%" SCNx8 "… rest of format string", &mac[0], … rest of arguments);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6813 次 |
| 最近记录: |