By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

pass-by-value

P: n/a
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

Oct 11 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
xdevel said:
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?
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)
Oct 11 '06 #2

P: n/a

xdevel wrote:
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.

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

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.

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.

Oct 11 '06 #3

P: n/a

ro***********@yahoo.com ha scritto:
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 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?

Oct 11 '06 #4

P: n/a

Richard Heathfield ha scritto:
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.
so in C exists only a pass-by-value mechanism in fact also if we pass a
pointer we are passing a copy of it while in C++ exists a pass-by-value
and a pass-by-reference mechanism

Oct 11 '06 #5

P: n/a
On Wed, 11 Oct 2006 07:18:17 +0000, Richard Heathfield wrote:
>xdevel said:
>But if I pass a pointer what really is happening?

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.
Sometimes called 'pass by address'.

Best regards,
Roland Pibinger
Oct 11 '06 #6

P: n/a

xdevel wrote:
ro***********@yahoo.com ha scritto:
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 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

Why not just:

void swap(int &a, int &b)
{
int tmp;

tmp = a;
a = b;
b = tmp;
}

This at least matches the pointer based variant below.
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?

Typically a reference parameter in a function call works internally
just like a pointer. I'd expect the generated code to generally be
identical. Using the swap example above, calling the reference version
with swap(x, y) would likely generate the identical code as calling the
pointer version with swap(&x, &y). Inside the reference version of
swap, the line a=b will essentially be transformed into *(A)=*(B) by
the compiler, where A and B are the internal pointers representing the
a and b parameters.

Of course the actual code generated by any particular compiler is
entirely up to the whim of the developers, and there's no reason they
cannot generate different code for the two cases.

References add no new functions to C++, but do allow a consistent way
to access member data and functions. That makes it possible to define
new types that have similar syntactic characteristics as the basic
types.

Oct 11 '06 #7

P: n/a
xdevel said:
>
Richard Heathfield ha scritto:
>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.

so in C exists only a pass-by-value mechanism
Correct.
in fact also if we pass a pointer we are passing a copy of it
More precisely, we are passing its value.

<off-topic stuff snipped>

--
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)
Oct 11 '06 #8

P: n/a
Roland Pibinger said:
On Wed, 11 Oct 2006 07:18:17 +0000, Richard Heathfield wrote:
>>xdevel said:
>>But if I pass a pointer what really is happening?

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.

Sometimes called 'pass by address'.
Why confuse the issue? The pointer's value is passed. In this respect,
expressions involving pointers and used as arguments are no different to
any other expressions that are used as arguments. There is no need to
invent a special terminology for pointers-as-arguments.

--
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)
Oct 11 '06 #9

P: n/a
Roland Pibinger wrote:
On Wed, 11 Oct 2006 07:18:17 +0000, Richard Heathfield wrote:
>>xdevel said:
>>But if I pass a pointer what really is happening?

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.

Sometimes called 'pass by address'.
I've never heard pass-by-value of a pointer value be called
`pass by address`. I /have/ heard `pass [or call] by reference`
be called `pass by address`.

C's evaluation rules for arguments are the same as its evaluation
rules for expressions: it doesn't have special magic for pointer
expressions. (The nearest it has to a special pointer magic is that
array names evaluate to the address of their first element.)

--
Chris "Essen -8 and counting" Dollin
A rock is not a fact. A rock is a rock.

Oct 11 '06 #10

P: n/a
On 11 Oct 2006 00:50:17 -0700, in comp.lang.c , "xdevel"
<xd********@yahoo.comwrote:
>and so my question is why if pass-by-reference can be considerd as a
pass-by-pointer is the code different?
Since References are C++, you need to ask over in comp.lang.c++.

By the way, pass by reference CANT be considered as by pointer since,
as you observed, they have different semantics and effects. Anyone who
says they're equivalent has not thought it through.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 11 '06 #11

P: n/a
xdevel wrote:
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
?
C function parameters are passed by value. There are *no* exceptions.

When you pass a pointer, the pointer is passed by value.

If you want to think of your design in terms of pointers being
references to some data, that is acceptable, but the mechanics of the
function cal are still, strictly, pass-by-value.

Say you have a pointer to a struct. You can call that a reference to
the struct, if it helps you. But when you pass that reference, the
reference itself is passed by value.
Oct 11 '06 #12

P: n/a
>ro***********@yahoo.com ha scritto:
>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.
But this is exactly what "pass by reference" *is*: a method by
which the language hides the "implicit pointers", so that the
programmer does not have to write them out (and, in general, *cannot*
omit them either).

In article <11**********************@c28g2000cwb.googlegroups .com>
xdevel <xd********@yahoo.comwrote:
>ok but if I write a [C++] swap function with reference I can do this:
void swap(int &a, int &b)
{
int temp = 0;
int &tmp = temp;
You do not need this extra reference -- you can just write "int tmp;".
tmp = a;
a = b;
b = tmp;
}
here I can swap the objects directly
Not only that, you can*not* swap the pointers that the implementation
secretly uses internally.
>and instead with pointer I can do this:
void swapP(int *a, int *b)
{
int tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
and in fact, if all you have are pointers that you pass by value,
you *must* write this.
>I can't swap directly the pointers...
You *can*; it just has no effect on the caller, because the
pointer values in swapP() are copies of the values supplied
by the caller.
>and so my question is why if pass-by-reference can be considerd as a
pass-by-pointer
is the code different?
Might as well ask "why, if horses can be considered as a zebra,
is the code different?" They may be related, but they are not
the same thing. (In fact, "by-pointer" is not a Term of Art in
compiler circles, while "by-reference" is. The three common
mechanisms available to Fortran compilers, for instance, are
called "value", "reference", and "value-result". There is also
a "call by name" mechanism, using "thunks", for Algol.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Oct 11 '06 #13

P: n/a
av
On 11 Oct 2006 01:20:28 -0700, ro***********@yahoo.com wrote:
>xdevel wrote:
>ro***********@yahoo.com ha scritto:
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 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


Why not just:

void swap(int &a, int &b)
{
i like to write "swap(&a, &b);" because so
i see that swap() function change "a" and "b"

in what i see reference in c++ is useful for class operators functions
for write something like c=a+b; (a, b, are reference in "+" operator
the same for "=" operator; evenif not change a and b
there is not overead for pass pointers (like reference) to stucts in
"+" operator (otherwise it seems will be &c=&a+&b or a, b, c have to
be pointers for write "c=a+b;" ) )
Oct 11 '06 #14

P: n/a
xdevel wrote:
ro***********@yahoo.com ha scritto:
>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 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...
You could if you do:

void swapP(int **a, int **b)
{
int *tmp;

tmp = *a;
*a = *b;
*b = tmp;
}
Oct 12 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.