Changing pointer inside function?

This is a discussion on Changing pointer inside function? within the C++ Programming forums, part of the General Programming Boards category; Hey, probably a simple question for some of you... I need to pass an NULL pointer to a function and ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    158

    Changing pointer inside function?

    Hey, probably a simple question for some of you...

    I need to pass an NULL pointer to a function and have the pointer be given a value within the function. How would that be done? Can a pointer be passed as reference?

    My code would look something like this:

    Code:
    void GetData(DataType *data)
    {
    data = (DataType *) FunctionThatReturnsPointer();
    }
    
    DataType *data = NULL;
    GetData(data);
    But this is not changing the value of the NULL pointer for me after the function exits...

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Can a pointer be passed as reference?
    Yes.

    Code:
    void GetData(DataType*& data);
    Incidentally, if the pointer is always supposed to be NULL, why not?
    Code:
    DataType* GetData();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    158

    Thumbs up

    Quote Originally Posted by anon View Post
    Yes.

    Code:
    void GetData(DataType*& data);
    Alright, that is working... great! Thanks.

    Quote Originally Posted by anon View Post
    Incidentally, if the pointer is always supposed to be NULL, why not?
    Code:
    DataType* GetData();
    The reason I can't do that is because I need to pass several pointers into the function and only assign values to some of them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM

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