CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2002
    Posts
    130

    How to pass the pointer back through function parameter

    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!

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Code:
    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.....
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Jul 2002
    Posts
    130
    Hi Graham

    It is great and works perfect without changing my code.

    Thanks!

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  5. #5
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured