开发者

Passing arrays vs structures to functions

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 ?


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜