c ++ passing a pointer into a function

This is a discussion on c ++ passing a pointer into a function within the C++ Programming forums, part of the General Programming Boards category; What is the difference between passing a pointer* of an object to a function; and passing &object to a function?...

  1. #1
    Ron
    Ron is offline
    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,041
    & 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
    Ron
    Ron is offline
    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
    839
    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
    21,313
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04:01 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08: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

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