What is the difference between passing a pointer* of an object to a function; and passing &object to 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?...
What is the difference between passing a pointer* of an object to a function; and passing &object to a function?
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 *)
& 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.
You know what date is on this coin? 1958. It's been travelling twenty two years to get here.
And now it's here.
And I'm here.
And it's either heads or tails.
And you have to say...
Call it.
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)
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.
So, basically you are asking about the difference between passing an object by reference and passing a pointer to it by value?So can I change the contents of the struct object with * or &?
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.Also, in the function definition, the object will be defined with its class name or struct 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