473,485 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

pass-by-value

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
14 2435
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

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

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

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
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

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
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
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
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
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
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
>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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1313
by: uli2003wien | last post by:
Dear group, PASS (SQL-Server user group) is about to start founding a branch in Vienna. I went to SQLCON and met some guys from the German PASS group and decided to become a member of PASS,...
8
2596
by: Tcs | last post by:
I've been stumped on this for quite a while. I don't know if it's so simple that I just can't see it, or it's really possible. (Obviously, I HOPE it IS possible.) I'm trying to get my queries...
2
15188
by: Robert | last post by:
when using the following function to create a pass through query is there a way to set the query property, "Returns Rows" to no. The default is yes. Since we are planning to create the pass...
7
21580
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
2
17040
by: Alex Nitulescu | last post by:
Hi. I have tried to pass two parameters, like this: Response.Redirect(String.Format("NewPage.aspx?Username={0}, Pass={1}", txtUserName.Text, txtPass.Text)) But if I pass Username="Alex" and...
3
5462
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
4
2227
by: Marcelo | last post by:
Any suggestion? Thanks Marcelo
3
12566
by: ILCSP | last post by:
Hello, I'm fairly new to the concept of running action pass through queries (insert, update, etc.) from Access 2000. I have a SQL Server 2000 database and I'm using a Access 2K database as my...
5
6302
by: marshmallowww | last post by:
I have an Access 2000 mde application which uses ADO and pass through queries to communicate with SQL Server 7, 2000 or 2005. Some of my customers, especially those with SQL Server 2005, have had...
5
15496
by: Remote_User | last post by:
Hi, Is there any way I can pass a file as a parameter? ( maybe read it into a object) I am working on C# VS2005. Thanks.
0
7090
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6960
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
4857
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4551
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1376
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
595
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.