此c程序中的格式错误警告

mri*_*dra 4 c

我试图在没有警告的情况下编译该程序,

#include<stdio.h>
int main()
{
    int arr[] = {1,2,3};
    printf("value1 = %d value2 %d\n", *(&arr+1), *(arr+1));//here is the warning
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是我收到编译时警告

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)

我正在64位ubuntu机器gcc版本4.6.3(Ubuntu/Linaro 4.6.3-1ubuntu5)编译器上编译我的程序.

Lun*_*din 6

&arr获取数组的地址,它是一个数组指针(不要与指向第一个元素的指针混淆).它有类型int(*)[3].

然后你对这个数组指针进行指针运算.因为它指向一个由3个整数组成的数组,所以&arr + 1意味着"加上一个完整数组的大小",你最终指向刚刚声明的数组,这没有任何意义.

然后你用数组指针的内容*.然后,您将再次获取阵列.当您在表达式中使用数组时,它会衰减为指向其第一个元素的指针int*.哪个不兼容int,因此错误.

我猜你可能正在尝试做任何一件事,&arr[0] + 1或者arr + 1两者都意味着同样的事情.