* ptr的[10]和(* PTR)[10]之间差 [英] Difference between *ptr[10] and (*ptr)[10]
问题描述
有关以下code:
int (*ptr)[10];
int a[10]={99,1,2,3,4,5,6,7,8,9};
ptr=&a;
printf("%d",(*ptr)[1]);
我应该将其打印?我在这里期待垃圾值,但输出 1
。结果
(为此我得出的结论是初始化这样指针数组即 PTR [10]
将开始指向 A [10] $的元素C $ C>按顺序)。
What should it print? I'm expecting the garbage value here but the output is 1
.
(for which I'm concluding that initializing this way pointer array i.e ptr[10]
would start pointing to elements of a[10]
in order).
但这个code片段:
int *ptr[10];
int a[10]={0,1,2,3,4,5,6,7,8,9};
*ptr=a;
printf("%d",*ptr[1]);
这是给分段错误。
It is giving the segmentation fault.
推荐答案
为int * PTR [10];
这是10 为int *
指针,而不是你会以为,一个指向10数组 INT $ C数组$ C>取值
This is an array of 10 int*
pointers, not as you would assume, a pointer to an array of 10 int
s
INT(* PTR)[10];
这是一个指向10数组 INT
This is a pointer to an array of 10 int
这是我相信一样为int * PTR;
中既可以指向数组,但指定的形式也只能点到10阵列 INT
取值
It is I believe the same as int *ptr;
in that both can point to an array, but the given form can ONLY point to an array of 10 int
s
这篇关于* ptr的[10]和(* PTR)[10]之间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!