【发布时间】:2016-01-25 06:44:55
【问题描述】:
我正在尝试编译这个程序而没有警告,
#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;
}
但是我收到了编译时警告
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
我正在 64 位 ubuntu 机器 gcc 版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) 编译器上编译我的程序。
【问题讨论】:
-
*(&arr+1)-->arr的地址(作为指向3个ints数组的指针),偏移3个ints数组的大小,然后取消引用得到一个包含 3 个ints 的数组,然后衰减到指向int的指针(它指向arr的原始开头之后的第4 个int,或第2 个数组的第一个intints 起始地址为arr)。
标签: c