467,879 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,879 developers. It's quick & easy.

address of return value

Hi,

How can I get the address of return value in function?

ex.

int function(int argument)
{
int *arg_address;
int *rtn_address;

arg_address = &argument;
rtn_address = ??;

Mar 31 '06 #1
  • viewed: 2917
Share:
11 Replies
may be,you can do as following:

int ss(int i)
{
int *k = &i;
int *j = &i;
return (int)j; //this is a cast,becase j is a type of
int*
}

Mar 31 '06 #2
maybe,you can as following:
int ss(int i)
{
int *k = &i;
int *j = &i;
return (int)j; //this is a cast;

}

Mar 31 '06 #3
ig*********@gmail.com wrote:
Hi,

How can I get the address of return value in function?


It is not necessarily the case that the return value even has an
address. For sufficiently small types your compiler may choose to place
the return value in a register.

Alan
Mar 31 '06 #4
Hello,

ig*********@gmail.com wrote:
How can I get the address of return value in function?

Plain and simple answer, you can't. The location of the return value is
completely in the hands of the compiler, whether register or stack or
whatever. There are no syntactical means for influencing the return
value location or taking its address.

This would be helpfull to print return value automatically (when
object is destroyed).


I don't understand this, maybe you could provide a more complete
example, what you want to achieve, especially what it has to do with
destruction of an object.
Bernd

Mar 31 '06 #5
Getting an address of return value in function, why?

I think it's illegal, just silly.

The variable has been beyond its scope, but you want to use it by its'
pointer.

If i were you , I would never do that.

Mar 31 '06 #6

ig*********@gmail.com wrote:
Hi,

How can I get the address of return value in function?


Make the function take the address to store the return value into:

void f(int* result)
{
// here results is the address of the return value
*result = -1;
}

Mar 31 '06 #7
Well, it would be usefull for tracing. Destructor of object would print
return value automatically (when object goes out of scope - at the
return from function).

You can always do:
int f(int a)
{
int ret = 0;
int *ret_addr = &ret;
Apr 1 '06 #8
igor.....
do..u want to print..the address...of
variable...or....exact...locataion......of...retur n...value..location

Apr 1 '06 #9
I want to print return value automatically, without doing cout on every
return. This could be done if one has address of return variable or
address of temporary variable (above example), which is always set to
return value before actually returning. Then create an temporary
object, which prints this value at destruction.

Hope its clearer now. Should have give more info to start with... My
bad.

Regards,
Igor

Apr 2 '06 #10
I want to print return value automatically, without doing cout on every
return. This could be done if one has address of return variable or
address of temporary variable (above example), which is always set to
return value before actually returning. Then create an temporary
object, which prints this value at destruction.

Hope its clearer now. Should have give more info to start with... My
bad.

Regards,
Igor

Apr 2 '06 #11

ig*********@gmail.com wrote:
I want to print return value automatically, without doing cout on every
return. This could be done if one has address of return variable or
address of temporary variable (above example), which is always set to
return value before actually returning.


How about a suitable #define RETURN?

As an added bonus, you can make it print only in debug mode.

HTH,
Michiel Salters

Apr 3 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Pushker Pradhan | last post: by
35 posts views Thread by hasho | last post: by
27 posts views Thread by Adam Warner | last post: by
11 posts views Thread by Brian Henry | last post: by
65 posts views Thread by kyle.tk | last post: by
11 posts views Thread by !truth | last post: by
reply views Thread by jack112 | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.