我们为什么不能传递数组按值的功能? [英] Why can't we pass arrays to function by value?

查看:222
本文介绍了我们为什么不能传递数组按值的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,我们可以通过复杂的类实例功能,但为什么我们不能传递数组功能?

Apparently, we can pass complex class instances to functions, but why can't we pass arrays to functions?

推荐答案

起源是历史。问题在于规则数组衰变为指针,传递给函数时,很简单。

The origin is historical. The problem is that the rule "arrays decay into pointers, when passed to a function" is simple.

复制阵列将是一种复杂的,不是很清楚,因为行为会为不同的参数和不同的函数声明更改。

Copying arrays would be kind of complicated and not very clear, since the behavior would change for different parameters and different function declarations.

请注意,您仍然可以通过值来间接通:

Note that you can still do an indirect pass by value:

struct A { int arr[2]; };
void func(struct A);

这篇关于我们为什么不能传递数组按值的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆