【发布时间】:2016-11-27 09:09:33
【问题描述】:
这背后有什么历史或逻辑原因吗?
说明: 当您将数组传递给 C 中的函数时,实际上您只传递了一个指向数组的指针。 但是,当您传递结构时,您可以传递结构的副本或指针。
//this:
int function(int array[10])
// is equivalent to this:
int function(int *array)
//and they both pass a pointer
//but this:
int function(struct tag name)
//passes a struct by value, where as this:
int function(struct tag *name)
//passes a pointer to it.
为什么不一样?
【问题讨论】:
-
可以按值传递数组,但数组的值是指向其第一个元素的指针(不是指向数组的指针)。
-
如果我没记错的话,指向结构的指针也是指向它的第一个值的指针
-
不,它们是不同的(类型不匹配,仅此而已)。
-
我认为这个问题更多地属于programmers.SE。值得注意的是,K&R C 不允许值传递/返回
structs。
标签: c arrays struct pass-by-value