传递数组VS结构功能 [英] Passing arrays vs structures to functions

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

问题描述

在C,数组传递给函数的指针。结构可以通过值或地址(指针)传递给函数。有什么具体的原因,为什么我们不能按值传递数组,但我们可以通过值传递结构法?

In C, arrays are passed to functions as pointers. Structures can be passed to functions either by value or by address (pointer). Is there any specific reason why we can not pass array by value but we can pass structre by value ?

推荐答案

在C,的所有的是按值传递。有,指出在大多数情况下,一个阵列的名称是相当于一个指针到它的第一个元素的另一个规则。将数组传递给一个函数是这样一个背景下。

In C, everything is passed by value. There is another rule that says that in most contexts, the name of an array is equivalent to a pointer to its first element. Passing an array to a function is such a context.

因此​​,特殊情况不说,数组是通过引用传递,特殊情况下的有关数组腐烂的指针规则。这使一个IM pression一个数组通过引用传递(它是有效的,但现在你知道为什么!)

So, the special case is not that arrays are passed by reference, the special case is the rule about arrays decaying to pointers. This gives one the impression that an array is passed by reference (which it effectively is, but now you know why!)

以上我的链接的文章解释约在不同环境下的数组类型的更多细节。

The post in my link above explains in more detail about the type of an array in different contexts.

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

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