【发布时间】:2021-12-12 21:54:23
【问题描述】:
基于约定 int (*o)[5]=&s;是指针 o 指向具有 5 个元素的数组的正确方法。
我们也可以在这个语句中写这个s
int (*p)[10]=s;
但为什么更喜欢
&s at int (*o)[5]=&s;
因为它们都返回相同的输出。
#include <stdio.h>
int main()
{
int s[5]={10,1,2,3,4};
int (*p)[10]=s;
printf("%d\n",*p);
printf("%d\n",**p);
printf("%d\n",&s);
printf("\n");
int (*o)[5]=&s;
printf("%d\n",*o);
printf("%d\n",**o);
printf("%d",&s);
return 0;
}
这个程序的输出是:
-593812272
10
-593812272
-593812272
10
-593812272
【问题讨论】:
-
一个体面的编译器应该为
int (*p)[10]=s;生成一个警告:`警告:不兼容的指针类型初始化'int (*)[5]' 使用'int [5]' 类型的表达式;用 & [-Wincompatible-pointer-types]`获取地址
标签: arrays c pointers printf pointer-to-array