Hi i have the following code in c under Linux platform
你好,我在Linux平台下有以下c语言的代码
#include <stdio.h>
#include <string.h>
int main(void) {
unsigned char client_id[8];
char id[17] = "00000001";
sscanf(id,"%02X%02X%02X%02X", &client_id[0], &client_id[1], &client_id[2], &client_id[3]);
printf("%02X%02X%02X%02X", client_id[0], client_id[1], client_id[2], client_id[3]);
}
When I run this code I get the output
当我运行这段代码时,我得到了输出
00000001
but with following warning
但由于以下警告
> test.c: In function ‘main’: test.c:7:1: warning: format ‘%X’ expects
> argument of type ‘unsigned int *’, but argument 3 has type ‘unsigned
> char *’ [-Wformat=]
> sscanf(id,"%02X%02X%02X%02X",&client_id[0],&client_id[1],&client_id[2],&client_id[3]);
> ^ test.c:7:1: warning: format ‘%X’ expects argument of type ‘unsigned
> int *’, but argument 4 has type ‘unsigned char *’ [-Wformat=]
> test.c:7:1: warning: format ‘%X’ expects argument of type ‘unsigned
> int *’, but argument 5 has type ‘unsigned char *’ [-Wformat=]
> test.c:7:1: warning: format ‘%X’ expects argument of type ‘unsigned
> int *’, but argument 6 has type ‘unsigned char *’ [-Wformat=]
The warnings are but obvious but how can I remove them?
这些警告是显而易见的,但我如何才能删除它们呢?
I do not want to use
我不想用
-Wno-pointer-sign
-Wno-pointer-sign
Should I use sprintf
or any changes in sscanf
?
我应该使用sprintf还是sscanf中的任何更改?
1 个解决方案
#1
3
You need to tell sscanf
that the arguments are of a char
type. This is done by putting the hh
modifier before the X
format specifier:
您需要告诉sscanf参数是char类型的。通过将hh修饰符放在X格式说明符之前完成:
sscanf(id,"%02hhX%02hhX%02hhX%02hhX",&client_id[0],&client_id[1],&client_id[2],&client_id[3]);
#1
3
You need to tell sscanf
that the arguments are of a char
type. This is done by putting the hh
modifier before the X
format specifier:
您需要告诉sscanf参数是char类型的。通过将hh修饰符放在X格式说明符之前完成:
sscanf(id,"%02hhX%02hhX%02hhX%02hhX",&client_id[0],&client_id[1],&client_id[2],&client_id[3]);