为什么在尝试显示整数存储位置时会出现clang错误? [关闭]

问题描述 投票:-3回答:1

我查看了与我的问题相符的问题,但找不到答案。在创建一个显示整数'i'和'k'的内存位置的程序后,它没有使用clang进行编译。使用SoloLearn的IDE时效果很好。

#include <stdio.h>

void test(int k);

int main() {
    int i = 0;

    printf("The address of i is %x\n", &i);
    test(i);
    printf("The address of i is %x\n", &i);
    test(i);

    return 0;
}

void test(int k) {
    printf("The address of k is %x\n", &k);
}

这些是我得到的错误。

memory.c:8:37: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
        printf("The address of i is %x\n", &i);
                                    ~~     ^~
memory.c:10:37: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
        printf("The address of i is %x\n", &i);
                                    ~~     ^~
memory.c:17:37: warning: format specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
        printf("The address of k is %x\n", &k);
                                    ~~     ^~
3 warnings generated.

我是否需要签署int,如果是,我该怎么办?

c integer
1个回答
2
投票

如果要打印变量或内存位置的地址,则应使用%p格式说明符。例如

int i = 0;
printf("The address of i is %p\n", (void*)&i);/* %p format specifier expects argument of void* */ 

来自C标准:

(C11,7.21.6.1p8格式化输入/输出函数)“p参数应为指向void的指针。”

© www.soinside.com 2019 - 2024. All rights reserved.