按值传递数组 [英] Pass array by value

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

问题描述




我刚学习C / C ++中的数组/指针二元性。我无法b / b $ b帮助想知道,有没有办法按值传递数组?好像

唯一的方法就是通过引用传递它?


谢谢,

BB

Hi,

I am just learning about the array/pointer duality in C/C++. I couldn''t
help wondering, is there a way to pass an array by value? It seems like
the only way to do is to pass it by reference??

Thanks,
BB

推荐答案

Berk Birand写道:
Berk Birand wrote:


我只是在学习数组/指针的二元性在C / C ++中。我无法帮助想知道,有没有办法按值传递数组?看来
唯一的方法就是通过引用传递它?

谢谢,
BB
Hi,

I am just learning about the array/pointer duality in C/C++. I couldn''t
help wondering, is there a way to pass an array by value? It seems like
the only way to do is to pass it by reference??

Thanks,
BB



当一个数组时是用C或C ++传递的(不同的,经常是与b + b不兼容的语言和公共子集),它会在第一个元素中衰减成指针

;指针按值传递。


模拟按值传递数组的唯一方法是将它包装在一个结构中。




HTH,

- $

-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子) 12/5)
http://www.cafepress.com/goldsays


When an array is passed in either C or C++ (different, oft subtly
incompatible languages with a common subset), it decays into a pointer
to its first element; the pointer is passed by value.

The only way to simulate passing an array by value would be to wrap it
in a struct.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


不要使用数组 - 请改用std :: vector。

" Berk Birand" < GR ****** @ yahoo.com> skrev i en meddelelse

news:1107492506.5f08d4c39f894327dfa88bc7c283a9ce@t eranews ...
Do not use an array - use std::vector instead.
"Berk Birand" <gr******@yahoo.com> skrev i en meddelelse
news:1107492506.5f08d4c39f894327dfa88bc7c283a9ce@t eranews...


我刚学习数组/指针二元性在C / C ++中。我无法帮助想知道,有没有办法按值传递数组?看来
唯一的办法就是通过引用传递它?
如果你能避免它们,也不要使用指针。
谢谢,
BB
Hi,

I am just learning about the array/pointer duality in C/C++. I couldn''t
help wondering, is there a way to pass an array by value? It seems like
the only way to do is to pass it by reference?? Also do not use pointers if you can avoid them.
Thanks,
BB



Peter Koch Larsen写道:
Peter Koch Larsen wrote:
不要使用数组 - 改为使用std :: vector。
" Berk Birand" < GR ****** @ yahoo.com> skrev i en meddelelse
新闻:1107492506.5f08d4c39f894327dfa88bc7c283a9ce@t eranews ...
Do not use an array - use std::vector instead.
"Berk Birand" <gr******@yahoo.com> skrev i en meddelelse
news:1107492506.5f08d4c39f894327dfa88bc7c283a9ce@t eranews...


我刚学习数组/指针二元性在C / C ++中。我无法帮助想知道,有没有办法按值传递数组?看来
唯一的方法就是通过引用传递它?
Hi,

I am just learning about the array/pointer duality in C/C++. I couldn''t
help wondering, is there a way to pass an array by value? It seems like
the only way to do is to pass it by reference??



如果可以避免它们也不要使用指针。



Also do not use pointers if you can avoid them.

谢谢,
BB
Thanks,
BB





感谢您的回答。另外一个问题是我的

介意是否在函数头中使用引用或指针。是

更好的做法是宣布一个函数,比如说:


void afunc(int& i);


或者更好地将其声明为


void afunc(int * i);


我知道在第二种情况下,每当有人想要访问它时,必须明确地取消引用
。但是这可以被认为是一个很好的事情,因为它使你更明显地操纵指针,

并且值会发生变化。另一方面,这只是更多的工作!


所以,你怎么说呢?



Thanks to both for your answers. Also another question that pops in my
mind is whether to use references or pointers in function headers. Is it
better practice to declare a function as, say:

void afunc(int& i);

or is it better to declare it as

void afunc(int* i);

I know that in the second case, one has to explicitly dereference i
whenever one wants to access it. But this can be considered a good
thing, as it makes it more obvious that you are manipulating pointers,
and the values are going to change. On the other hand, it''s just more work!

So, what do you say about this?


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

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