Thread: Pass pointer to function

  1. #1
    Registered User
    Join Date
    Nov 2005

    Pass pointer to function


    I am trying to pass a pointer to a function that accepts a reference to an object. Can someone tell me how to do this?
    My code looks something like the following:

    UserMatrix mat;
    UserMatrix *ptrMat;
    ptrMat = &mat;
    void setUpMat(UserMatrix &matrix)
        matrix[0] = 1;
    So how can I call the function setUpMatrix with ptrMat??

    Thanks for your help

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    setUpMat ( *ptrMat );

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Why would you need a pointer, when you're already passing by reference.

    Given your current function, you would do
    setUpMat( mat );

    There's no need for the pointer.

  4. #4
    Registered User
    Join Date
    Apr 2003
    I am trying to pass a pointer to a function that accepts a reference to an object.
    You can't. C++ requires that the arguments you send to a function be the same type as the parameter type. A pointer type and a reference type are not the same types.

    As Slacker showed, you can dereference the pointer to get the object at that address and then send that object to the function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. how to pass function pointer as arg in c++
    By umen242 in forum C++ Programming
    Replies: 13
    Last Post: 10-21-2008, 02:09 AM
  4. Replies: 7
    Last Post: 07-04-2007, 12:46 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM