Thread: Changing pointer inside function?

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

    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
    166

    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, 03: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, 02: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