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[i];
s[i] = c;
}
}
==========================================
Output
==========================================
before str = hello
after str = olleh
before k = 10
after k = 10 4 1770
On 4 Apr 2005 16:38:38 -0700 jr********@hotmail.com wrote: 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 jr********@hotmail.com wrote: 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);
--
Best regards,
Andrey Tarasevich
On Mon, 04 Apr 2005 17:17:10 -0700
Andrey Tarasevich <an**************@hotmail.com> wrote:
[...] 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.
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.
On Mon, 04 Apr 2005 20:45:35 -0400, Daniel C. Bastos wrote: On Mon, 04 Apr 2005 17:17:10 -0700 Andrey Tarasevich <an**************@hotmail.com> wrote:
.... 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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mark Dicken |
last post by:
Hi All
I have found the following Microsoft Technet 'Q'
Article :-
Q210368 -ACC2000: How to Pass an Array as an Argument to a Procedure
(I've also copied and pasted the whole contents into...
|
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...
|
by: z_learning_tester |
last post by:
I'm reading the MS press C# book and there seems to be a contradiction.
Please tell me which one is correct, 1 or 2.
Thanks!
Jeff
1. First it gives the code below saying that it prints 0 then...
|
by: John Bailo |
last post by:
I want to pass a SqlCommand object as a input parameter to a method.
I want to pass the SqlCommand "by value" so that any updates to the
original object are *not* reflected in the object within...
|
by: kinaxx |
last post by:
Hello,
now I'm learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism
1) pass by value (inother words : call by value)
2)...
| |
by: kinaxx |
last post by:
Hello,
now I'm learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism
1) pass by value (inother words : call by value)
2)...
|
by: Bryan Parkoff |
last post by:
I write my large project in C++ source code. My C++ source code
contains approximate four thousand small functions. Most of them are
inline. I define variables and functions in the global scope....
|
by: raylopez99 |
last post by:
I'm posting this fragment from another thread to frame the issue
clearer.
How to pass an object to a function/method call in C# that will
guarantee not to change the object?* In C++, as seen...
|
by: raylopez99 |
last post by:
Keywords: scope resolution, passing classes between parent and child
forms, parameter constructor method, normal constructor, default
constructor, forward reference, sharing classes between forms....
|
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,...
|
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...
| |
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |