Click to See Complete Forum and Search --> : How to pass the pointer back through function parameter


neil0lee
September 13th, 2002, 04:02 AM
Hello,

I have a question about function parameter passing:

In my program, there is a pointer pointing to an image, say

CImage * pImage;

The program needs to call a function to do something.

void f( CImage * pImg );


What I want is that: if pImage is not null, function f(...) just uses it; if function f(...) finds pImg is null, it can load image and pass the pointer to image back to my program through a parameter. It seems that my above parameter passing can not do it.

Can anybody help me to solve the problem and how should I pass the parameters?

Thanks a lot!

Graham
September 13th, 2002, 04:06 AM
void f(CImage*& pImage)
{
if (pImage == 0) // Don't use NULL
{
pImage = // whatever
}
// carry on
}

There will now begin a long debate on the merits of reference-to-pointer verus pointer-to-pointer.....

neil0lee
September 13th, 2002, 04:44 AM
Hi Graham

It is great and works perfect without changing my code.

Thanks!

PaulWendt
September 13th, 2002, 06:14 AM
Originally posted by neil0lee
Hi Graham

It is great and works perfect without changing my code.

Thanks!

Although I typically prefer the client syntax of pointer-to-pointer
argument passing, this statement speaks for itself :)

--Paul

jfaust
September 13th, 2002, 10:39 AM
But that same statement is also the danger. The same usage will produce different results.

I still prefer **, but I really don't fee too strongly about either way. I do dislike the *& notation.

Jeff