通过按值 [英] pass-by-value

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

问题描述

我需要你的帮助,因为我不太了解这一点:

C参数中的
按值传递。函数参数得到一个副本

的参数值。

但是如果我传递一个指针究竟发生了什么?还有一份副本通过



$ C $ b在C ++中也有一个传递参考...在这种情况下

参数可以被认为是参数的一个

别名...


但是当我们传递一个指针时现在在C中我们可以想到它作为一个

传递地址机制来操纵

变量?


谢谢

Hi, I need your help because I don''t understand very well this:

in C arguments are passed by-value. The function parameters get a copy
of the argument values.
But if I pass a pointer what really is happening? also a copy is passed
?

in C++ there is a pass-by-reference too... and in that case the
paramter can be considered as an
alias of the argument...

but now in C when we pass a pointer can we think of it as a
pass-by-address mechanism to manipulate
variables?

Thanks

推荐答案

xdevel说:
xdevel said:

我需要你的帮助因为我不太了解这个:

C参数中的
按值传递。
Hi, I need your help because I don''t understand very well this:

in C arguments are passed by-value.



正确。

Correct.


函数参数获取参数值的副本。
The function parameters get a copy of the argument values.



更准确地说,参数是*表达式*。参数表达式是

被评估,并且这些值存储在函数的参数中。

More precisely, arguments are *expressions*. The argument expressions are
evaluated, and those values are stored in the parameters to the function.


但是如果我传递指针真的是什么正在发生?
But if I pass a pointer what really is happening?



指针被计算,指针'的值存储在函数的

参数中,与任何参数一样其他论证表达。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)

The pointer is evaluated, and that pointer''s value is stored in the
parameter to the function, just the same as any other argument expression.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)




xdevel写道:

xdevel wrote:

我需要你的帮助,因为我不太了解这一点:


in C arguments按值传递。函数参数获得参数值的副本


Hi, I need your help because I don''t understand very well this:

in C arguments are passed by-value. The function parameters get a copy
of the argument values.



这是正确的。


That''s correct.


但是如果我传递指针真的是什么正在发生?也是副本通过


But if I pass a pointer what really is happening? also a copy is passed
?



传递*指针*的副本。它指向的是*不*

复制。


A copy of the *pointer* is passed. The thing it''s pointing to is *not*
copied.


在C ++中有一个传递-inference也是......在这种情况下,

参数可以被认为是参数的一个

别名...
in C++ there is a pass-by-reference too... and in that case the
paramter can be considered as an
alias of the argument...



虽然OT在这里,C ++引用实际上只是一种不同的方式来引用

到指针。隐藏&的只是语法糖。 (在

电话中)和*() (在函数内部)来自你的运算符。


While OT here, C++ references are really just a different way to refer
to pointers. It''s just syntactic sugar that hides the "&" (at the
call) and "*()" (inside the function) operators from you.


但是当我们传递指针时现在在C中我们可以把它想象成一个

传递地址机制来操纵

变量?
but now in C when we pass a pointer can we think of it as a
pass-by-address mechanism to manipulate
variables?



如果我正确地解释你的问题,是的。传递的指针

将指向原始对象,原始对象可能是通过该指针访问的



If I''m interpreting your question correctly, yes. The passed pointer
will point to the original object, and the original object may be
accessed via that pointer.

< br>


ro *********** @ yahoo。 com ha scritto:

ro***********@yahoo.com ha scritto:

虽然OT在这里,C ++引用实际上只是一种不同的方式来引用

到指针。隐藏&的只是语法糖。 (在

电话中)和*() (在函数内部)来自你的运营商。
While OT here, C++ references are really just a different way to refer
to pointers. It''s just syntactic sugar that hides the "&" (at the
call) and "*()" (inside the function) operators from you.



ok但是如果我用引用写一个交换函数我可以这样做:

void swap(int& a,int& b)

{

int temp = 0;

int& tmp = temp;


tmp = a;

a = b;

b = tmp;

}

这里我可以交换对象直接


而不是指针我可以这样做:

void swapP(int * a,int * b)

{

int tmp;


tmp = * a;

* a = * b;

* b = tmp;

}

我不能直接交换指针......


所以我的问题这就是为什么如果通过引用可以考虑作为

传递指针

代码是不同的?


什么类型的指针是参考?

ok but if I write a swap function with reference I can do this:
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;

tmp = a;
a = b;
b = tmp;
}
here I can swap the objects directly

and instead with pointer I can do this:
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
I can''t swap directly the pointers...

and so my question is why if pass-by-reference can be considerd as a
pass-by-pointer
is the code different?

what type of pointer is a reference?


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

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