为什么我们不能将& array传递给有可能& array [0]的函数 [英] why cant we pass &array to function where &array[0] is possible
本文介绍了为什么我们不能将& array传递给有可能& array [0]的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
void fun(int* array){}
int main(){
int array[]={1,2,3};
fun(&array);----->(1)//error in this line
return 0;
}
错误:无法将参数"1"的"int(*)[3]"转换为"int *",以避免"fun(int *â)".
如果我通过了 fun(& array [0])
,则可以正常工作.根据我的理解,& array
和& array [0]
产生相同的地址.请澄清.
if i am passing fun(&array[0])
,its working fine.As per my understanding &array
and &array[0]
both yield the same address.Kindly clarify.
推荐答案
尽管保证任何聚合的地址(这意味着数组和标准布局类)与第一个元素的地址相同,但类型是不同,并且没有隐式转换.
While the address of any aggregate (this means arrays and standard-layout classes) is guaranteed to be the same as the address of the first element, the types are different, and there is no implicit conversion.
作为一个示例,为什么其他类型如此重要和有用:
As an example why the different type is important and useful:
for( int *p = array; p < (&array)[1]; ++p )
迭代 array
的所有元素,而
for( int *p = array; p < (&array[0])+1; ++p )
只执行一次.
这篇关于为什么我们不能将& array传递给有可能& array [0]的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文