Address of a result?

J

JustSomeGuy

I have a function that takes a pointer as an argument...
for example:

void myFunc(unsigned int * parameter)


how can I call myFunc with the result of a function without defining a
variable
to hold the parameter... ie.

main()
{
myFunc(&AnotherFunc()); // Wont work..
int p = AnotherFunc();
myFunc(&p); // Will work.
}

Will using a reference rather than a pointer help here?
 
A

Alf P. Steinbach

I have a function that takes a pointer as an argument...
for example:

void myFunc(unsigned int * parameter)


how can I call myFunc with the result of a function without defining a
variable
to hold the parameter... ie.

main()
{
myFunc(&AnotherFunc()); // Wont work..
int p = AnotherFunc();
myFunc(&p); // Will work.
}

Will using a reference rather than a pointer help here?

That depends entirely on what myFunc does.

If you can use


void myFunc( usigned parameter )


that would be best.

If you can use


int* AnotherFunc()


that is second best.

Otherwise consider

void myFuncWrapper( int x )
{
myFunc( &x );
}
 
M

Moonlit

Hi,

JustSomeGuy said:
I have a function that takes a pointer as an argument...
for example:

void myFunc(unsigned int * parameter)


how can I call myFunc with the result of a function without defining a
variable
to hold the parameter... ie.
What does AnotherFunc returns. If it returns a pointer to something
allocated on the heap do this:

MyFunc( AnotherFunc() );

If it just returns the value of a local:

int AnotherFunc()
{
int I = 10;
return I;
}

Then you have to store the temporary:
Like this:

int P;
MyFunc( &( P = AnotherFunc() ) );

Or change the function myFunc:

void MyFunc( unsigned int parameter );

MyFunc( AnotherFunc() );

Since it doesn't make sence to change a temporary, why is it a pointer in
the first place?


main()
{
myFunc(&AnotherFunc()); // Wont work..
int p = AnotherFunc();
myFunc(&p); // Will work.
}

Will using a reference rather than a pointer help here?

No you have to store the returned temporary somewhere;
Regards Ron AF Greve
 
J

JustSomeGuy

Moonlit said:
Hi,


What does AnotherFunc returns. If it returns a pointer to something
allocated on the heap do this:

MyFunc( AnotherFunc() );

If it just returns the value of a local:

int AnotherFunc()
{
int I = 10;
return I;
}

Then you have to store the temporary:
Like this:

int P;
MyFunc( &( P = AnotherFunc() ) );

Or change the function myFunc:

void MyFunc( unsigned int parameter );

MyFunc( AnotherFunc() );

Since it doesn't make sence to change a temporary, why is it a pointer in
the first place?
Because it needs to change the contents of the value passed.
 
M

Moonlit

Hi,

JustSomeGuy said:
Because it needs to change the contents of the value passed.

The value will be gone at the end of the function call, so you have to store
it in a variable anyway.

Reagrds, Ron AF Greve
 
J

JustSomeGuy

Moonlit said:
Hi,



The value will be gone at the end of the function call, so you have to store
it in a variable anyway.

Reagrds, Ron AF Greve

a variable I won't use...
I mean what is wrong with passing the address of the autovariable on the
return stack?
 
M

Moonlit

JustSomeGuy said:
defining

a variable I won't use...
I mean what is wrong with passing the address of the autovariable on the
return stack?

How are you going to use the value that is the result from that operation
then? It is gone once the function returns.

What is wrong with using a variable? What exactly do you mean with
autovariable (automatic one i.e. a variable in your local routine or a
temporary one i.e. one that has disappeared at the end of your statement?

Regards, Ron AF Greve.
 
R

Rolf Magnus

JustSomeGuy said:
a variable I won't use...

On one hand, your function must change the contents, on the other hand,
you don't want to do anything with the result of that change? That
doesn't make a lot of sense to me.
I mean what is wrong with passing the address of the autovariable on
the return stack?

The problem is that this address couldn't be used for anything.
 
D

Darkay Li

pointer is the address of memory.
your function "AnotherFunc" return a value, the compile can't get the
address of it(coz the function will return a value by a register etc.)!
when you use p, then the compilke known the address is the address of
variable "p"
So...
 

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

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top