为什么我们不能通过值传递数组来函数? [英] Why can't we pass arrays to function by value?

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

问题描述

显然,我们可以将复杂的类实例传递给函数,但是为什么我们不能将数组传递给函数呢?

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天全站免登陆