sscanf()函数中不兼容的参数警告。

时间:2021-10-11 06:59:01

Getting this error when performing make:

执行时出现此错误:

$ make
cc -O2 -g src/bittwist.c -o src/bittwist -I/usr/local/include -L/usr/local/lib -lpcap
cc -O2 -g src/bittwiste.c -o src/bittwiste -I/usr/local/include -L/usr/local/lib -lpcap
src/bittwiste.c: In function ‘main’:
src/bittwiste.c:99:21: warning: format ‘%x’ expects argument of type ‘unsigned int *’, but argument 3 has type ‘u_char *’ [-Wformat=]
                     sscanf(optarg, "%02x", &payload_opt[i]);
                     ^

Here is the code:

这是代码:

payload_len_opt = (u_short)c / 2; / possible resizing in editing functions /
payload_opt = (u_char *)malloc(sizeof(u_char) *payload_len_opt);
if (payload_opt == NULL)
error("malloc(): cannot allocate memory for payload_opt"); / make a byte of data from every 2 characters of optarg /
for (i = 0; i < payload_len_opt; i++) {
/ ugly - let me know if there is a better way to achieve this /
sscanf(optarg, "%02x", &payload_opt[i]);

I'm using Ubuntu 14.04.2 LTS

我使用Ubuntu 14.04.2 LTS。

1 个解决方案

#1


3  

First, in your code

首先,在您的代码中

payload_opt = (u_char )malloc(sizeof(u_char)  payload_len_opt);

looks very wrong, because

看起来非常错误的,因为

  1. Standard Warning : Please do not cast the return value of malloc() and family in C. If u_char is not of a pointer type, you're into deep trouble. So, better, don't cast at all. It is not at all required.

    标准警告:请不要将malloc()和家庭的返回值转换为c。如果u_char不是指针类型,则会陷入严重的麻烦。所以,最好不要打石膏。这根本不需要。

  2. Even in case, your u_char is a pointer type, maybe a typedef to unsigned char *, you're missing a multiplication (*) operator there. in c, sizeof(char) is guaranteed to be 1, so you can just write

    即使是这样,您的u_char也是一个指针类型,可能是一个未签名的char *类型,您缺少一个乘法(*)操作符。在c中,sizeof(char)被保证为1,所以您可以只写。

    payload_opt = malloc(payload_len_opt);
    
  3. The multiline comment syntax is /*...*/, not / ... / (missing *)

    多行注释语法是/*…* /,/……失踪/(*)

That said, regarding the error message, for %x format specifier, from C11, chapter §7.21.6.2

关于错误消息,% x格式说明符,从C11、章§7.21.6.2

....The corresponding argument shall be a pointer to unsigned integer.

....对应的参数应该是一个指向无符号整数的指针。

but in your case, from the error message, it looks like payload_opt is of type u_char *.

但是在您的示例中,从错误消息来看,它看起来像payload_opt类型的u_char *。

#1


3  

First, in your code

首先,在您的代码中

payload_opt = (u_char )malloc(sizeof(u_char)  payload_len_opt);

looks very wrong, because

看起来非常错误的,因为

  1. Standard Warning : Please do not cast the return value of malloc() and family in C. If u_char is not of a pointer type, you're into deep trouble. So, better, don't cast at all. It is not at all required.

    标准警告:请不要将malloc()和家庭的返回值转换为c。如果u_char不是指针类型,则会陷入严重的麻烦。所以,最好不要打石膏。这根本不需要。

  2. Even in case, your u_char is a pointer type, maybe a typedef to unsigned char *, you're missing a multiplication (*) operator there. in c, sizeof(char) is guaranteed to be 1, so you can just write

    即使是这样,您的u_char也是一个指针类型,可能是一个未签名的char *类型,您缺少一个乘法(*)操作符。在c中,sizeof(char)被保证为1,所以您可以只写。

    payload_opt = malloc(payload_len_opt);
    
  3. The multiline comment syntax is /*...*/, not / ... / (missing *)

    多行注释语法是/*…* /,/……失踪/(*)

That said, regarding the error message, for %x format specifier, from C11, chapter §7.21.6.2

关于错误消息,% x格式说明符,从C11、章§7.21.6.2

....The corresponding argument shall be a pointer to unsigned integer.

....对应的参数应该是一个指向无符号整数的指针。

but in your case, from the error message, it looks like payload_opt is of type u_char *.

但是在您的示例中,从错误消息来看,它看起来像payload_opt类型的u_char *。