passing reference to pointer

Discussion in 'C++' started by softdeveloper007, Feb 15, 2009.

  1. Hi,

    I am stuck here and need some inputs. To cut it short and simple:

    void func1( )
    {
    int* p1, p2;
    ....

    func2 (p1, p2);
    ...

    }

    void func2 (int*& pt1, int*& pt2)
    {

    }

    I got: "error LNK2001: unresolved external symbol...fatal error
    LNK1120: 1 unresolved externals"

    but problem is gone by simply changing it to ptr to ptr:

    void func1( )
    {
    int* p1, p2;
    ....

    func2 (&p1, &p2);
    ...

    }

    void func2 (int** pt1, int** pt2)
    {

    }

    compiler: visual studio6.0.

    Any pointer, please? thanks!
    softdeveloper007, Feb 15, 2009
    #1
    1. Advertising

  2. softdeveloper007

    Gareth Owen Guest

    softdeveloper007 <> writes:

    > void func1( )
    > {
    > int* p1, p2;


    This doesn't declare two pointers, it declares a p1 as a
    pointer-to-int and p2 as an int.

    int *p1, *p2;

    does what you want. It's good practice to group the * with the
    variable name for precisely that reason.
    Gareth Owen, Feb 15, 2009
    #2
    1. Advertising

  3. softdeveloper007

    Bo Persson Guest

    Gareth Owen wrote:
    > softdeveloper007 <> writes:
    >
    >> void func1( )
    >> {
    >> int* p1, p2;

    >
    > This doesn't declare two pointers, it declares a p1 as a
    > pointer-to-int and p2 as an int.
    >
    > int *p1, *p2;
    >
    > does what you want. It's good practice to group the * with the
    > variable name for precisely that reason.


    And an even better practice is to only define one variable at a time.

    int* p1 = 0;
    int* p2 = 0;


    Bo Persson
    Bo Persson, Feb 15, 2009
    #3
  4. guys, thanks for all your inputs.

    first of all, i'd like to apologize for the typo of missing * ahead of
    p2 declaration that causes confusion.

    i tried to make it as simple as possible. the original problem was
    bubble-sorting the linked list by passing two pointers to nodes to a
    swapping function.

    it seems to me that, in order to properly step in the original
    pointers thru the list, the two pointers should be passed by value
    instead of reference to pointers,in other words, the pointers in the
    caller would remain pointing to the same memory location, and not be
    affected by swapping function, sounds right, right?

    but it seemed, the pace that the pointer walks in the caller is
    affected by the swapping function and thus steps in 2 steps (instead
    of 1) each time due to some reason.

    i knew simply swapping the value instead of re-arranging the nodes
    would be a much simpler way but that's not my choice here due to other
    restrictions.

    thanks.

    On Feb 15, 4:11 am, "Bo Persson" <> wrote:
    > Gareth Owen wrote:
    > > softdeveloper007 <> writes:

    >
    > >> void func1( )
    > >> {
    > >> int* p1, p2;

    >
    > > This doesn't declare two pointers, it declares a p1 as a
    > > pointer-to-int and p2 as an int.

    >
    > > int *p1, *p2;

    >
    > > does what you want. It's good practice to group the * with the
    > > variable name for precisely that reason.

    >
    > And an even better practice is to only define one variable at a time.
    >
    > int* p1 = 0;
    > int* p2 = 0;
    >
    > Bo Persson
    softdeveloper007, Feb 16, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    527
    E. Robert Tisdale
    Jul 16, 2003
  2. jimjim
    Replies:
    16
    Views:
    822
    Jordan Abel
    Mar 28, 2006
  3. pereges
    Replies:
    7
    Views:
    423
    pereges
    Jun 1, 2008
  4. A
    Replies:
    7
    Views:
    626
  5. Replies:
    2
    Views:
    131
Loading...

Share This Page