pass by value

M

mdh

May I just confirm my understanding that:

If a pointer is passed as an argument to a function, manipulation of
the function pointer ( eg pointer ++ till some condition....) will not
effect the value of the calling pointer?

So;

myf(p) /* p is ptr to char) */


void myf( char *q){

while (...)

*q++;

}

leaves p unchanged?
 
O

osmium

mdh said:
May I just confirm my understanding that:

If a pointer is passed as an argument to a function, manipulation of
the function pointer ( eg pointer ++ till some condition....) will not
effect the value of the calling pointer?

So;

myf(p) /* p is ptr to char) */


void myf( char *q){

while (...)

*q++;

}

leaves p unchanged?

Yes, that's right. One nitpick, I wouldn't use "function pointer" in this
discussion, it usually means something else.
 
R

Richard Heathfield

mdh said:
May I just confirm my understanding that:

If a pointer is passed as an argument to a function, manipulation of
the function pointer ( eg pointer ++ till some condition....) will not
effect the value of the calling pointer?

So;

myf(p) /* p is ptr to char) */


void myf( char *q){

while (...)

*q++;

}

leaves p unchanged?

It does indeed leave p unchanged. It doesn't change anything in the
thing pointed to, either, though. Were you aware of that? It is
possible that *q++; doesn't do quite what you think it does.
 
R

Richard

Richard Heathfield said:
mdh said:


It does indeed leave p unchanged. It doesn't change anything in the
thing pointed to, either, though. Were you aware of that? It is
possible that *q++; doesn't do quite what you think it does.

In this sample code that is misleading.

the "..." could have been anything manipulating exactly what p was
pointing to.
 
R

Richard Heathfield

Richard said:
In this sample code that is misleading.

No, it isn't.
the "..." could have been anything manipulating exactly what p was
pointing to.

Indeed, but we can only go on the code that is posted. My point refers
specifically to the statement: *q++;
 
M

mdh

Richard said:



Nope...but then I was just "making up" some code to be sure I
understood "pass by value" and in the past, I have had my knuckles
rapped for doing this!!! :)
As I look at the hasty code I wrote, I think what this would do would
simply point at subsequent values of an array and not change a
thing!!! The wonder of terse code :)
 

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

Similar Threads

pass by address 42
pointer to function question 5
Array of structs function pointer 10
pass by reference 15
struct by value 54
pass by value 10
Can't solve problems! please Help 0
Pass by reference? 7

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top