Passing a pointer as a reference

This is a discussion on Passing a pointer as a reference within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a function name fun1(), in which there is a pointer say ptr1. I am passing this pointer ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    17

    Passing a pointer as a reference

    Hi,

    I have a function name fun1(), in which there is a pointer say ptr1. I am passing this pointer to fun2() and in fun2() there are some changes that are being made to that pointer (say some increment or allocation to some structure).

    I want that modification to be reflected in fun1(), i don't want to return it from fun2(). What i want is that fun2() receive this pointer as a reference to original pointer and any changing made to it will be reflected in fun1().

    i am doing this
    Code:
    void fun1()
    {
        someStruct *ptr = null;
        fun2(ptr);
        .
        .
        .
        ptr -> someThing = 3;
    }
    
    void fun2 (someStruct &ptr)
    {
        ptr = new someStruct();
        ptr++;
    }
    but it is not working ... .

    can you people help me ... please

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > ptr++;
    Incrementing the pointer itself, after you've allocated it is a sure way of messing up what happens next.

    Code:
    void fun1()
    {
        someStruct *ptr = null;
        fun2(ptr);
        .
        .
        .
        ptr -> someThing = 3;
    }
    
    void fun2 (someStruct &ptr)
    {
        ptr = new someStruct();
        ptr->omeThing = 0;
    }
    If fun2 is prototyped properly before you use it, should be ok.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To the OP:

    Your code doesn't work because it's not valid C. It's C++. C doesn't have references.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Quote Originally Posted by quzah
    To the OP:

    Your code doesn't work because it's not valid C. It's C++. C doesn't have references.


    Quzah.
    Or the new keyword either for that matter.
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Moved to C++
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Or the null keyword for that matter. Unless you've defined null yourself, you should use NULL or just plain 0.

    Code:
    void fun2 (someStruct &ptr)
    {
        ptr = new someStruct();
        ptr->omeThing = 0;
    }
    That code mixes pointers and references . . . it should be
    Code:
    void fun2 (someStruct *ptr)
    {
        ptr = new someStruct();
        ptr->omeThing = 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 08:21 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. How to: Use OpenGL with Jgrasp
    By Pickels in forum Game Programming
    Replies: 3
    Last Post: 08-30-2005, 10:37 AM

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