CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 18

Threaded View

  1. #10
    Join Date
    Apr 2010
    Posts
    20

    Re: looking for a good pointer info

    Paul is right. If you use the address of operator you are passing a pointer which is by value to the function.

    You pass by reference doing the following.

    Code:
    void Function1(int & x)
    {
        // notice the & after the type of variable before the name of the variable
       x += 100;
    }
    If you were passing a pointer by reference like the below, the address that the pointer points at could be modified.

    Code:
    void Function2(int *& pX)
    {
        pX += 100; // dangerous, adds 100 integer widths to the pointer variable, not the integer it points at.
    }
    Which is essentially doing the same thing as a pointer to a pointer, just the address cannot change.

    Code:
    void Function3(int ** pX)
    {
        *pX += 100; // we dereference the pointer to a pointer to get a reference to the pointer, this is just the same as the above code.
    }
    Last edited by CppCoder2010; July 27th, 2011 at 11:06 AM.

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