将数组通过值传递到函数
以下是本书 c编程的片段。这不是错误的,因为数组永远无法按价值传递?
viii.6:如何通过值将数组传递给函数?
答案:一个数组可以通过在 称为函数数组名称 使用方括号(<代码> [和
]
) 附在末端。打电话时 功能,只需通过 阵列(即阵列的名称) 到所谓的函数。例如, 以下程序通过数组x []
到名称的功能byval_func()
按值:
int []
参数告诉 编译器byval_func()
功能将有一个论点 - 一个 整数阵列。什么时候byval_func()
函数,您 将阵列的地址传递到byval_func()
:byval_func(x);
因为数组正在传递 值,数组的确切副本为 制作并放在堆栈上。这 称为功能然后接收到此 阵列的副本可以打印。 因为阵列传递给
byval_func()
是 原始数组,修改数组 在byval_func()
函数中 对原始数组没有影响。
Below is a snippet from the book C Programming Just the FAQs. Isn't this wrong as Arrays can never be passed by value?
VIII.6: How can you pass an array to a function by value?
Answer: An array can be passed to a function by value by declaring in
the called function the array name
with square brackets ([
and]
)
attached to the end. When calling the
function, simply pass the address of
the array (that is, the array’s name)
to the called function. For instance,
the following program passes the arrayx[]
to the function namedbyval_func()
by value:The
int[]
parameter tells the
compiler that thebyval_func()
function will take one argument—an
array of integers. When thebyval_func()
function is called, you
pass the address of the array tobyval_func()
:byval_func(x);
Because the array is being passed by
value, an exact copy of the array is
made and placed on the stack. The
called function then receives this
copy of the array and can print it.
Because the array passed tobyval_func()
is a copy of the
original array, modifying the array
within thebyval_func()
function has
no effect on the original array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是不正确的:数组本身没有被复制,只有指向其地址的指针的副本传递给Callee(放在堆栈上)。 (无论您是将参数声明为
int []
还是int*
,它 衰减到指针 。)这使您可以从呼叫函数内部修改数组的内容。因此,这个是错误的(对@Jonathan Leffler的评论很荣幸)。但是,重新分配函数内部的指针不会将指针更改为函数之外的原始数组。
This is incorrect: the array itself is not being copied, only a copy of the pointer to its address is passed to the callee (placed on the stack). (Regardless of whether you declare the parameter as
int[]
orint*
, it decays into a pointer.) This allows you to modify the contents of the array from within the called function. Thus, thisis plain wrong (kudos to @Jonathan Leffler for his comment below). However, reassigning the pointer inside the function will not change the pointer to the original array outside the function.
燃烧那本书。如果您想要一个没有初学者程序员编写的真实C FAQ,请使用此: http:http:http: //c-faq.com/aryptr/index.html 。
语法方面,严格地说您 在C中通过值传递数组,
但是 ,对于记录,C中有一个肮脏的技巧,它确实允许您通过数组根据C的价值。不要在家尝试!因为尽管有这个技巧,但仍然永远没有理由按值传递数组。
Burn that book. If you want a real C FAQ that wasn't written by a beginner programmer, use this one: http://c-faq.com/aryptr/index.html.
Syntax-wise, strictly speaking you cannot pass an array by value in C.
However, for the record there is a dirty trick in C that does allow you to pass an array by value in C. Don't try this at home! Because despite this trick, there is still never a reason to pass an array by value.
确切地。您不能在C中按值来通过一个数组。
我看了书的引用部分,这种混乱或错误的来源很快找到。
作者不知道
*i
等于i []
作为函数的参数时。后一种形式是为了明确说明代码的读者的发明,表明i
指向一个数组,这是一个很好的混乱来源,也是这个问题的骗子。我觉得很有趣的是,本书的特定部分或其他部分的作者(因为本书有 5 的作者)或 7 < /strong>校对者至少没有提及句子:
至少这样,他们应该注意到存在冲突。
由于您通过地址,它只是一个地址。没有任何神奇的事情会将地址变成一个全新的数组。
但是回到问题本身:
您似乎无法通过C中的数组来通过C中的值,因为您似乎已经知道自己。但是您可以做三个(可能还有更多,但这是我的无罪状态),这可能是根据独特的情况的替代方法,所以让我们开始。
Exactly. You cannot pass an array by value in C.
I took a look at the quoted part of the book and the source of this confusion or mistake is pretty fast found.
The author did not know about that
*i
is equivalent toi[]
when provided as a parameter to a function. The latter form was invented to explicitly illustrate the reader of the code, thati
points to an array, which is a great source of confusion, as well-shown by this question.What I think is funny, that the author of the particular part of the book or at least one of the other parts (because the book has 5 authors in total) or one of the 7 proofreaders did not mentioned at least the sentence:
With at least that, they should had noticed that there is a conflict.
Since you passing an address, it is only an address. There is nothing magically happen which turns an address into a whole new array.
But back to the question itself:
You can not pass an array as it is by value in C, as you already seem to know yourself. But you can do three (there might be more, but that is my acutal status of it) things, which might be an alternative depending on the unique case, so let´s start.
在C和C ++中,不可能通过一个值将完整的内存块作为参数传递到一个函数,但是我们可以传递其地址。实际上,这几乎具有相同的效果,并且更快,更有效地操作。
为了安全起见,您可以将数组尺寸传递或在指针之前放置const预选赛,以确保Callee不会更改它。
In C and C++ it is NOT possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address. In practice this has almost the same effect and it is a much faster and more efficient operation.
To be safe, you can pass the array size or put const qualifier before the pointer to make sure the callee won't change it.
Yo可以通过将阵列包裹到结构中来解决它
Yuo can work it around by wrapping the array into the struct
通过这种方法,我们可以按值将数组传递,但实际上该数组正在通过其实际复制在堆栈中的基本地址访问。
By this method we can pass the array by value, but actually the array is accessing through the its base address which actually copying in the stack.