pointer argument receiving address in c++? - c++

int y=5;
int *yPtr = nullptr;
yPtr = &y;
I understand that the pointer stores the address of y. and calling *yPtr dereferences y.
If I have a call to the void function:
int main()
{
int number = 5;
function( &number );
}
void function( int *nPtr)
{
*nPtr = *nPtr * *nPtr;
}
if the function takes a pointer as argument, how can the call to the function use an address?
I understand that nPtr stores addresses but why couldn't it be defined as.
void functions (int &ref)
{
ref = ref * ref;
}
My main question would be: Why does a function receiving an address argument need a pointer parameter to receive the address?

By using a pass-by-reference parameter, you force your function not the copy the value of the parameter itself, instead, to use the actual variable you provide. So, for a more clear view, see below:
void function( int number )
{
cout << number;
}
function( myInt ); // function will copy myInt, into its local variables stack
but, by using the pass-by-reference method, like this:
void function ( int & number )
{
cout << number
}
function( myInt ); // function will not copy myInt into its local variables stack, instead, it will use the already existent myInt variable.
There is no difference in how to compiler will work with pass-by-pointer and pass-by-reference parameters. Instead, the call of your function will look like so:
void function_p( int *number )
{
cout << *number;
}
void function_r( int & number )
{
cout << number;
}
// and the calls
function_p( &myInt ); // it is required to use address-of operator here
function_r( myInt ); // the effect will be the same, but with less effort in writing that address-of operator
In C++11, programmers started to use pass-by-reference method, in general, ordinarily because it has an easier writing "template".
To complete the answer to your question, the * and & operators refer only to the type of the parameter, so that they create compound types. A compound type is a type that is defined in terms of another type. C++ has several compound types, two of which are references and pointers.
You can understand that they only affect the type of a variable (in our case, a parameter), by writing them in a proper way:
int* p1; // we will read this: pointer p1 points to an int
int* p2 = &var1; // we read this: pointer p2 points to int variable var1
int var1 = 12;
int& ref1 = var1; // and we read this: ref1 is a reference to var1
You can generally consider references represent a different for the same block of memory.

you did mean
void functions (int &ref)
{
ref = ref * ref;
}
that's how you use refs, avoiding all the '*'s of pointers syntax

This is another of those quirky things of C++. Passing by reference is not the same as passing a pointer. When you do something like
void functions (int &ref)
You can pass an actual variables into functions (rather than pointers to them) like
int a = 12;
functions(a);
And inside the function you have no need to dereference. Notice that you are passing by reference, rather than passing a reference.
When you pass a reference, or pointer to something, you use star as such
void function( int *nPtr)
And thus you have to dereference with a *
Note also that you get the reference to a variable (or constant) by putting & in front of it, but you declare a reference or pointer by putting a * in front of it. At the same time, you dereference a pointer also by putting a * in front of it.

Related

I don't understand what's the different between a '&' in and out of a class object's variable parentheses [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
This is confusing me:
class CDummy
{
public:
int isitme (CDummy& param);
};
int CDummy::isitme (CDummy& param)
{
if (&param == this)
{
return true; //ampersand sign on left side??
}
else
{
return false;
}
}
int main ()
{
CDummy a;
CDummy* b = &a;
if ( b->isitme(a) )
{
cout << "yes, &a is b";
}
return 0;
}
In C & usually means the address of a var. What does it mean here? Is this a fancy way of pointer notation?
The reason I am assuming it is a pointer notation because this is a pointer after all and we are checking for equality of two pointers.
I am studying from cplusplus.com and they have this example.
The & has more the one meanings:
1) take the address of a variable
int x;
void* p = &x;
//p will now point to x, as &x is the address of x
2) pass an argument by reference to a function
void foo(CDummy& x);
//you pass x by reference
//if you modify x inside the function, the change will be applied to the original variable
//a copy is not created for x, the original one is used
//this is preffered for passing large objects
//to prevent changes, pass by const reference:
void fooconst(const CDummy& x);
3) declare a reference variable
int k = 0;
int& r = k;
//r is a reference to k
r = 3;
assert( k == 3 );
4) bitwise and operator
int a = 3 & 1; // a = 1
n) others???
To start, note that
this
is a special pointer ( == memory address) to the class its in.
First, an object is instantiated:
CDummy a;
Next, a pointer is instantiated:
CDummy *b;
Next, the memory address of a is assigned to the pointer b:
b = &a;
Next, the method CDummy::isitme(CDummy &param) is called:
b->isitme(a);
A test is evaluated inside this method:
if (&param == this) // do something
Here's the tricky part. param is an object of type CDummy, but &param is the memory address of param. So the memory address of param is tested against another memory address called "this". If you copy the memory address of the object this method is called from into the argument of this method, this will result in true.
This kind of evaluation is usually done when overloading the copy constructor
MyClass& MyClass::operator=(const MyClass &other) {
// if a programmer tries to copy the same object into itself, protect
// from this behavior via this route
if (&other == this) return *this;
else {
// otherwise truly copy other into this
}
}
Also note the usage of *this, where this is being dereferenced. That is, instead of returning the memory address, return the object located at that memory address.
Well the CDummy& param that is declared as a parameter of the function CDummy::isitme is actually a reference which is "like" a pointer, but different. The important thing to note about references is that inside functions where they are passed as parameters, you really have a reference to the instance of the type, not "just" a pointer to it. So, on the line with the comment, the '&' is functioning just like in C, it is getting the address of the argument passed in, and comparing it to this which is, of course, a pointer to the instance of the class the method is being called on.

What does it mean to pass a pointer parameter by reference to a function?

I was reading some code online and I found them passing node*& instead of just node* and I didn't really understand why.
My understanding was that you pass parameters by reference when you either want your changes to sort of propagate back to the original variable outside of the function (which a pointer would do) or if you wanted to avoid copying the object every time the function is called to avoid the copying overhead which is also what a pointer would do.
Am I wrong or are they? If I am, what am I getting wrong?
When you pass by reference, it acts as an alias -- be it a normal object or a pointer.
To explain in detail, let's consider below declaration:
int i = 10, *pi = &i;
Now following are the different meanings of passing and using them in various functions:
void foo_pointer (int* p) // 1st (can pass `&i` and `pi`)
{
*p = 0; // modifies `i`
p = nullptr; // no effect on `pi`
}
void foo_pointer_reference (int*& p) // 2nd (can pass `pi`)
{
*p = 0; // modifies `i`
p = nullptr; // modifies `pi`
}
void foo_const_pointer_reference (const int*& p) // 3rd (can pass `pi`)
{
*p = 0; // ERROR
p = nullptr; // modifies `pi`
}
void foo_const_pointer_const_reference (const int* const& p) // 4th (can pass `&i` and `pi`)
{
*p = 0; // ERROR
p = nullptr; // ERROR
}
From above examples, you can see that another use case of declaring T*& as a function parameter is that, it restricts only pointer passing to that function. i.e. in above case &i is not allowed to be passed to 2nd and 3rd functions. However pi can be passed to all the functions.
You can pass a pointer by reference if you want the function you call to change the pointer to point to something else.
Other than that, I don't see any valid reason (but that is sometimes a valid reason).

Reason Behind Passing By reference to a Function in C++?

Here I'm Coding in C++.
int main()
{
int setAcapacity = 10;
int setANOE = 0;
int *setA;
createSet(&setA, setAcapacity);
}
void createSet(int **set, int n)
{
*set = new int[n];
}
can someone please explain it reason behind passing by reference to createSet Functions..Thanks in advance!!
At first, you do not pass by reference, you pass by pointer!
By reference would look like this:
int createSet(int*& set, int n)
// ^ reference to pointer
{
set = new int[n]; // assign to referenced value
}
createSet(setA, setAcapacity);
// ^ no need to take address
There are two reasons you would want to pass by reference or pointer:
The object to be passed is large, copying would be inefficient. In these cases, you typically pass a const reference or pointer to const.
The object passed shall be modified within the function. This is the case in your example, where a new value is assigned to the pointer setA outside the function which is passed via the pointer to named set.
What you do in int createSet(int **set, int n) you pass a pointer to a pointer; not a reference to a pointer. To pass a reference: int createSet(int * & set, int n).
In case of pass-by-reference, your code would looks like:
void createSet(int * & set, int n) // '&' for reference
{
set = new int[n];
}
int main()
{
int setAcapacity = 10;
int setANOE = 0;
int *setA = nullptr; // Better to initialize
createSet(setA, setAcapacity);
}
For your question: The difference between pass-by-pointer/reference and pass-by-value is that modifications made to arguments passed in by pointer/reference in the called function have effect in the calling function, whereas modifications made to arguments passed in by value in the called function can not affect the calling function. The #value# in your case is: #int-pointer#.
You pass a pointer/reference to the #value# because you want the assignment to target setA of the calling function: main(). That's why you also pass, in your original code, the address of setA. So new can store the new pointer in that address.
In addition, in many cases: Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.

Using Variable When Declaring Constructors [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
This is confusing me:
class CDummy
{
public:
int isitme (CDummy& param);
};
int CDummy::isitme (CDummy& param)
{
if (&param == this)
{
return true; //ampersand sign on left side??
}
else
{
return false;
}
}
int main ()
{
CDummy a;
CDummy* b = &a;
if ( b->isitme(a) )
{
cout << "yes, &a is b";
}
return 0;
}
In C & usually means the address of a var. What does it mean here? Is this a fancy way of pointer notation?
The reason I am assuming it is a pointer notation because this is a pointer after all and we are checking for equality of two pointers.
I am studying from cplusplus.com and they have this example.
The & has more the one meanings:
1) take the address of a variable
int x;
void* p = &x;
//p will now point to x, as &x is the address of x
2) pass an argument by reference to a function
void foo(CDummy& x);
//you pass x by reference
//if you modify x inside the function, the change will be applied to the original variable
//a copy is not created for x, the original one is used
//this is preffered for passing large objects
//to prevent changes, pass by const reference:
void fooconst(const CDummy& x);
3) declare a reference variable
int k = 0;
int& r = k;
//r is a reference to k
r = 3;
assert( k == 3 );
4) bitwise and operator
int a = 3 & 1; // a = 1
n) others???
To start, note that
this
is a special pointer ( == memory address) to the class its in.
First, an object is instantiated:
CDummy a;
Next, a pointer is instantiated:
CDummy *b;
Next, the memory address of a is assigned to the pointer b:
b = &a;
Next, the method CDummy::isitme(CDummy &param) is called:
b->isitme(a);
A test is evaluated inside this method:
if (&param == this) // do something
Here's the tricky part. param is an object of type CDummy, but &param is the memory address of param. So the memory address of param is tested against another memory address called "this". If you copy the memory address of the object this method is called from into the argument of this method, this will result in true.
This kind of evaluation is usually done when overloading the copy constructor
MyClass& MyClass::operator=(const MyClass &other) {
// if a programmer tries to copy the same object into itself, protect
// from this behavior via this route
if (&other == this) return *this;
else {
// otherwise truly copy other into this
}
}
Also note the usage of *this, where this is being dereferenced. That is, instead of returning the memory address, return the object located at that memory address.
Well the CDummy& param that is declared as a parameter of the function CDummy::isitme is actually a reference which is "like" a pointer, but different. The important thing to note about references is that inside functions where they are passed as parameters, you really have a reference to the instance of the type, not "just" a pointer to it. So, on the line with the comment, the '&' is functioning just like in C, it is getting the address of the argument passed in, and comparing it to this which is, of course, a pointer to the instance of the class the method is being called on.

c++ *& dereference address of operator?

In a recent assignment for a data structures class, we we're expected to use this "*&" in a function parameters list. I am having trouble finding info on why this exists, and even what it does. Is it standard practice to use this?
Here's a code snippet
template <class T>
Thing <T> * action(const T &dataIn, Thing <T> *& thingIn)
{
if(thingIn == NULL)
{
Node <T> *newThing = new Thing <T> (dataIn);
newThing->ptr = thingIn;
thingIn= newThing ;
return(newThing );
}
}
I guess another part of my question is, why use "*&" at all couldn't you just send the pointer?
It is used for a reference type pointing to a pointer
Example
int *&ref = ptr;
in the above example, ref is a reference to an int pointer ptr
which then can be passed in to a function
function(int *&ref)
fun(int x)
fun takes an int value
fun(int* x)
fun takes an int pointer by value
fun(int& x)
fun takes an int by reference
fun(int*& x)
fun takes an int pointer by reference - the value of the pointer to x as seen by the caller can be changed by fun
If you have a structure for example like
struct A
{
//...
};
then a function parameter can look like
void f( A * &pa );
In this declaration A * means pointer type and & means that this pointer is passed to the function by reference.
Compare these two code snippets
void f( A * &pa )
{
pa = new A();
}
//...
A *p = nullptr;
f( p );
After calling the function the variable p will have the address of the allocated memory in the function because it was passed to the function by reference.
If the function would be declared like this
void f( A * pa )
{
pa = new A();
}
//...
A *p = nullptr;
f( p );
then after calling the function the pointer p would not be changed because the function deals with a copy of the pointer.
let's look into this:
void fun( int i );
we pass parameter by value, it means function can use int i but cannot change variable that is used to pass that value:
void fun( int i ) { i = 123; }
int main()
{
int j = 0;
fun( j );
std::cout << j << std::endl; // j is still 0
}
on another side when we use refernce, variable would be changed:
void fun( int &i ) { i = 123; }
int main()
{
int j = 0;
fun( j );
std::cout << j << std::endl; // j is 123 now
}
if you understand this concept you should be able to undestand more complex types, just use typedef or more modern using:
typedef char * pchar;
fun( pchar &p );
fun( char *&p ); // this is the same as before
you can actually use it in program or at least in your mind, that should simplify your understanding that a pointer is the same variable as anything else. It also helps to understand other complex types like pointer to pointer etc.
In a recent assignment for a data structures class, we we're expected to use this "*&" in a function parameters list. I am having trouble finding info on why this exists, and even what it
does.
C++ declarations can be read from right to left. It's a reference to a pointer.
Is it standard practice to use this?
Not really. See below.
I guess another part of my question is, why use "*&" at all couldn't you just send the pointer?
A reference allows you to modify the object which is being referenced.
A pointer is an object. So a reference to a pointer allows you to modify a pointer. Modifying a pointer means to make it point somewhere else. So if you take a *& in a function, then it means that the function can change where the pointer outside of the function points to.
Changing the destination of a raw pointer is not something you do very often in C++, but as always, it depends on your application domain.
Changing the destination of an existing pointer certainly feels more like C-style programming, and there are no references in C. In C, you would use ** instead of *& to accomplish this, and pass a pointer to a pointer, accordingly.