sscanf()函数

时间:2015-06-18 13:12:00

标签: c

执行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]);
                     ^

以下是代码:

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]);

我正在使用Ubuntu 14.04.2 LTS

1 个答案:

答案 0 :(得分:3)

首先,在你的代码中

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

看起来非常错误,因为

  1. 标准警告:malloc() Cu_char家庭的返回值do not cast。如果u_char不是指针类型,那么你就会陷入困境。所以,更好的是,根本不投。根本不需要。

  2. 即使以下情况,您的unsigned char *是指针类型,也许是*的typedef,您在那里缺少乘法(c)运算符。在sizeof(char)中,1保证为payload_opt = malloc(payload_len_opt); ,因此您只需撰写

    /*...*/
  3. 多行注释语法为/ ... /,而非*(缺少%x

  4. 就是说,对于C11格式说明符的错误消息,来自payload_opt,章节§7.21.6.2

      

    ....相应的参数应该是指向   无符号整数。

    但在您的情况下,从错误消息中看来,u_char *的类型为$ docker run -d -p 7711:7711 ...