TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: KEVIN YOCHUM
from: DANIEL JONES
date: 1997-08-04 05:05:00
subject: Conversion of pointer

KY>  This was taken out of context.  It made a lot more sense in the
KY>  original post, however...
KY>  KY> void test( int i )
KY>  KY> {
KY>  KY> i=5;
KY>  KY> return;
KY>  KY> }
KY>  KY> You're not getting a pointer back,
KY>  CD> You aren't getting anything back!!
KY>  Sure you are!   You're getting the modified value of i.  In the abo
KY>  i would be an "in/out" parameter.
Nope, Chris is correct.  You're getting nothing back.  Not sure where
you got that "in/out" parameter stuff, but it's not working in the code
you posted.
When you call a function with an argument as you did, a COPY of that
argument is passed on the stack to the function.  The function can do
anything it likes to the copy, and the original is not affected.  Using
your definition of the test function above, this snippet:
int j = 10;
test(j);
printf("j = %i", j);
will print j = 10.
To get around this, there are a few things you can do.  One is return a
value from test(), and assign that value to j:
int test() //No argument is needed
{
    return 5;
}
j = test(); //Now j = 5
Alternatively, you can receive a pointer to j and modify j via that
pointer:
void test(int *i)
{
    *i = 5;
}
test(&J) //j = 5 here too
Finally, you can declare test() as accepting a reference:
void test(int &i) //Takes a reference to the argument
{
    i = 5;
    return;
}
KY>  CD> How about this one...
KY>  CD> int test()
KY>  CD> {
KY>  CD> return 5;
KY>  CD> }
KY>  CD> Kinda questionable in the utility department....
KY>  I was just using the code the original poster used.  In his example
KY>  was setting the variable to 5, so that's all I was doing.
But you weren't.  You were setting a local variable to 5, then
returning.  As soon as you return from the function, the local variable
is lost and the original value is unchanged.
Regards,
Daniel              ddjones@pinn.net
---
 þ RM 1.31 1604 þ ... I'm mooning you now. You just can't see me.
---------------
* Origin: Selective Source Virginia Beach, VA (757)471-6776 (1:275/102)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.