pass by value question

J

jrefactors

In the following program, are parameters s in function reverse() and
x in function count() both pass by value? How come value k
is not changed, but value str has changed?

Please advise.
thanks!!

============================================
void reverse(char s[]);
void count(int x);

int main(void)
{
char str[] = "hello";
int k = 10;
printf("before str = %s\n", str);
reverse(str);
printf("after str = %s\n", str);
printf("before k = %d\n", k);
count(k);
printf("after k = %d\n", k);
}

void count(int x)
{ x = x + 5;
}

void reverse(char s[])
{ int c, i, j;
for (i=0, j=strlen(s)-1; i<j; i++, j--)
{ c = s[j];
s[j] = s;
s = c;
}
}

==========================================
Output
==========================================
before str = hello
after str = olleh
before k = 10
after k = 10
 
D

Daniel C. Bastos

On 4 Apr 2005 16:38:38 -0700
In the following program, are parameters s in function
reverse() and x in function count() both pass by value? How
come value k is not changed, but value str has changed?

Please advise.
thanks!!

============================================
void reverse(char s[]);
void count(int x);

int main(void)
{
char str[] = "hello";
int k = 10;
printf("before str = %s\n", str);
reverse(str);

Here, you pass the value of the adress of the first element of
str[], then reverse() changes the array str. So it changes the
string "hello" pointed by str.

This is still passing by value, but since you pass the
memory address of where the array of chars is, reverse() changes
what's there.
printf("after str = %s\n", str);
printf("before k = %d\n", k);
count(k);

Here you pass the value of k, but since this is an integer,
count() adds 5 to x, so x = 15 in count() and when count()
returns, k hasn't changed.

To make count() change k outside, you could do count(int *x),
call count(&k) and do *x = *x + 5 inside count() and this will
change k. Notice that I write *x, I mean the value of whatever x
is pointing to.

I hope I'm correct here and I hope that clarifies. With the
changes I suggest, you'd get:

%./rev
before str = hello
after str = olleh
before k = 10
after k = 15
 
A

Andrey Tarasevich

In the following program, are parameters s in function reverse() and
x in function count() both pass by value?
Yes.

How come value k is not changed,

It is not supposed to. Arguments for parameters passed by value are
never affected by any changes made inside the function.
but value str has changed?

There's no way to pass an array "by value" in C. Declaration of
'reverse' can be rewritten in the following exactly equivalent manner

void reverse(char* s)

In this case the pointer 's' is passed by value. Regardless of what you
do to pointer 's' itself inside 'reverse', the actual argument will not
be affected (exactly like it is with 'k' and 'count'). However, if the
code inside 'reverse' modifies the array pointed to by 's', these
modifications will "persist" after 'reverse' returns. That's what you
observe in your code.

In other words, the pointer 's' is passed by value, but the array
pointed to by 's' is passed "by reference" (or "by pointer" to be exact).
void reverse(char s[]);
void count(int x);
 
D

Daniel C. Bastos

On Mon, 04 Apr 2005 17:17:10 -0700

[...]
It is not supposed to. Arguments for parameters passed by value
are never affected by any changes made inside the function.


There's no way to pass an array "by value" in C. Declaration of
'reverse' can be rewritten in the following exactly equivalent
manner

void reverse(char* s)

In this case the pointer 's' is passed by value. Regardless of
what you do to pointer 's' itself inside 'reverse', the actual
argument will not be affected (exactly like it is with 'k' and
'count'). However, if the code inside 'reverse' modifies the
array pointed to by 's', these modifications will "persist"
after 'reverse' returns. That's what you observe in your code.

That's a better explanation (compared to my previous).
In other words, the pointer 's' is passed by value, but the
array pointed to by 's' is passed "by reference" (or "by
pointer" to be exact).

I agree with this too, but didactically, I think it's just better
to avoid the word ``reference''. It's passed by value, period.
But if you pass a memory address and you're changing the data at
that address, it will persist.

Just my $0.02.
 
L

Lawrence Kirby

.


I agree with this too, but didactically, I think it's just better
to avoid the word ``reference''. It's passed by value, period.
But if you pass a memory address and you're changing the data at
that address, it will persist.

It is best to call a spade a spade. The array is simply not passed, C
never passes array arguments (although it can pass arrays hidden inside
structures or unions). What is passed (by value) is a pointer to the first
element of the array. The parameter in the called function behaves in all
respects like a pointer because that is what it is. There remains one
array i.e. the one in the caller and if the called function modifies array
elements through the pointer passed to it, that is the array it will
modify.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top