How to point correctly?

This is a discussion on How to point correctly? within the C++ Programming forums, part of the General Programming Boards category; How do I make ptr_1 pointing to the same address of ptr_2 after function_B is called? I tried doing this, ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    How to point correctly?

    How do I make ptr_1 pointing to the same address of ptr_2 after function_B is called? I tried doing this, but after function_B exits, ptr_1 points back to NULL. Thanks!

    Code:
    void cluster::function_A(void)
    {
    	int *ptr_1, *ptr_2;
    	ptr_1 = NULL;
    	ptr_2 = new int [ 10 ];
    	function_B(ptr_1, ptr_2);
    
    }
    
    void cluster::function_B(int *ptr_1, int *ptr_2)
    {
    	ptr_1 = ptr_2;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,772
    Pass ptr_1 by reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, i get it, i do this right?

    Code:
    void cluster::function_B(int *&ptr_1, int *&ptr_2)
    {
    	ptr_1 = ptr_2;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,772
    Yes, though in this example you do not need to pass ptr_2 by reference as it is not modified. Of course, this example is so simple that one would just write the assignment instead of calling the function
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, thanks a lot for guiding me, i found an excellent article on this issue =)

    http://www.codeproject.com/cpp/PtrToPtr.asp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why not use an = Operator instead of a Copy Constructor?
    By thetinman in forum C++ Programming
    Replies: 48
    Last Post: 10-15-2007, 03:58 PM
  2. MSDN OLE DB Sample Provider entry point
    By George2 in forum C++ Programming
    Replies: 0
    Last Post: 07-21-2007, 07:30 AM
  3. floating point operators
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2003, 07:53 PM
  4. Point passed rectangle...
    By Hunter2 in forum Game Programming
    Replies: 15
    Last Post: 10-10-2003, 09:57 AM
  5. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21