Thread: c ++ passing a pointer into a function

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    c ++ passing a pointer into a function

    What is the difference between passing a pointer* of an object to a function; and passing &object to a function?

  2. #2
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Passing a plain pointer to the function allows the function to alter the pointer's value.

    Passing &pointer to a function tells the function it's not alloud to change the value of the pointer, only use it's current value.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  3. #3

    Join Date
    May 2005
    Posts
    1,042
    & is either a reference or the operator that returns the memory address of a variable.

    A reference is basically a pointer that can only point to a single object (but you can modify the object that it points to).

    You can use a pointer to allocate memory.
    I'm not immature, I'm refined in the opposite direction.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    151
    So can I change the contents of the struct object with * or &? Also, in the function definition, the object will be defined with its class name or struct name?

    eg
    myString* myclass ::function(myString &string1)
    or
    myString* myclass ::function(myclass &string1)

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    a reference (type&) cannot be rebound to another location in memory. a pointer (type*) can. also to change the value stored at the location in memory of a pointer, you must dereference it (*pointerVariableName = value). this is not necessary with a reference. neither the value at nor the location of a const reference may be changed.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So can I change the contents of the struct object with * or &?
    So, basically you are asking about the difference between passing an object by reference and passing a pointer to it by value?

    Also, in the function definition, the object will be defined with its class name or struct name?
    Eh, what do you mean? myString is one type name, myclass is another type name, and they have nothing to do with each other unless one is an alias (due to typedef) of the another or both are aliases of another type name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 9
    Last Post: 12-25-2007, 05:01 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM