差异:& ary1D [0]和& ary1D [英] Difference: &ary1D[0] and &ary1D
问题描述
我的主函数中有一个双数组:
I have in my main-Function an double Array:
double ary1D[4] = {1.1, 2.2, 3.3, 4.4};
并进行函数调用:
print1D_A(&ary1D[0],Num);
我的功能:
void print1D_A(double *ary1D, int Num);
所以我的问题是,& ary1D [0] 和& ary1D 有什么区别.如果我使用& ary1D 调用函数,编译器会给我一个错误.但是参数& ary1D 是我数组的第一个地址,就像& ary1D [0] 一样.
so my question is, Whats is the difference between &ary1D[0] and &ary1D. My compiler give me an error, if I call the function with &ary1D. But the argument &ary1D is the first Adress of my Array, just like &ary1D[0].
我可以更改函数的参数列表吗,可以用
Can I change the argumentlist of my function, that I can call it with
print1D_A(&ary1D,Num);
推荐答案
表达式&ary1D[0]
是指向ary1D
的第一个元素的指针.其类型为double*
.这也是ary1D
衰变的原因,所以您可以这样做
The expression &ary1D[0]
is a pointer to the first element of ary1D
. Its type is double*
. This is also what plain ary1D
decays to, so you could just do
print1D_A(ary1D,Num);
表达式&ary1D
是指向 array 的指针.它的类型是double (*)[4]
.
The expression &ary1D
is a pointer to the array. Its type is double (*)[4]
.
现在,两个指针都指向相同的位置,但是如您所见,它们的类型非常不同.如果一个函数需要一种类型而您又给了另一种类型,则可能会导致错误.
Now, both pointers are pointing to the same location, but as you can see their types are very different. That could lead to errors if a function is expecting one type and you give the other.
这篇关于差异:& ary1D [0]和& ary1D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!