警告:格式“%x”需要类型为“unsigned int”的参数

warning: format ‘%x’ expects argument of type ‘unsigned int’

当我尝试编译它时出现以下错误,不知道为什么...

warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘char *’ [-Wformat=]

printf("Name buffer address:    %x\n", buffer);

代码:

#include <string.h>
#include <stdio.h>

main(){
        char name[200];
        printf("What is your name?\n");
        scanf("%s", name);
        bo(name, "uname -a");
}

int bo(char *name, char *cmd){
        char c[40];
        char buffer[40];
        printf("Name buffer address:    %x\n", buffer);
        printf("Command buffer address: %x\n", c);
        strcpy(c, cmd);
        strcpy(buffer, name);
        printf("Goodbye, %s!\n", buffer);
        printf("Executing command: %s\n", c);
        fflush(stdout);
        system(c);
}

要打印地址,请使用 "%p" 而不是 "%x"。您还需要转换为 void *

printf("Name buffer address:    %p\n", (void *) buffer);

您收到警告是因为以下语句

    printf("Name buffer address:    %x\n", buffer);
    printf("Command buffer address: %x\n", c);

%x 期望 unsigned int,而您提供的是指针。

参考,C11 标准,章节 §7.21.6.1

o,u,x,X
The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal notation (x or X) in the style dddd; [...]

提供无效参数调用 undefined behavior

您应该使用 %p 打印地址

p The argument shall be a pointer to void.[...]

并将参数转换为 void *,因为对于指针类型,不会发生 默认参数提升

话虽如此,

  1. main()至少应该是int main(void),才符合标准。
  2. 您需要转发声明您的函数 bo() 因为隐式声明 不好 和 non-standard 现在。