By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Address of a result?

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Thu, 18 Dec 2003 18:03:47 GMT, "JustSomeGuy" <no**@nottelling.com> wrote:
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 );
}
Jul 22 '05 #2

P: n/a
Hi,

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:7_lEb.744528$6C4.70208@pd7tw1no...
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
Jul 22 '05 #3

P: n/a

"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
Hi,

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:7_lEb.744528$6C4.70208@pd7tw1no...
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?

Because it needs to change the contents of the value passed.



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

Jul 22 '05 #4

P: n/a
Hi,

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:YqmEb.750925$9l5.617540@pd7tw2no...

"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
Hi,

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:7_lEb.744528$6C4.70208@pd7tw1no...
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?

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


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


Jul 22 '05 #5

P: n/a

"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
Hi,

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:YqmEb.750925$9l5.617540@pd7tw2no...

"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
Hi,

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:7_lEb.744528$6C4.70208@pd7tw1no...
> 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?
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


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



>
> 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



Jul 22 '05 #6

P: n/a

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:%cnEb.745040$6C4.318035@pd7tw1no...

"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
Hi,

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:YqmEb.750925$9l5.617540@pd7tw2no...

"Moonlit" <al******@jupiter.universe> wrote in message
news:3f*********************@news.xs4all.nl...
> Hi,
>
> "JustSomeGuy" <no**@nottelling.com> wrote in message
> news:7_lEb.744528$6C4.70208@pd7tw1no...
> > 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?
>
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


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.

>
>
> >
> > 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
>
>



Jul 22 '05 #7

P: n/a
JustSomeGuy wrote:
> > 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.
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...


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.

Jul 22 '05 #8

P: n/a
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...

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:7_lEb.744528$6C4.70208@pd7tw1no...
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?

Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.