This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 9 years ago.
To elaborate the title,
what is the difference between
book& a = b;
and
book* a = &b;
After learning C, these class declaration is really confusing me. Can anyone explain how these two line of codes work individually?
first one the reference variable and second one is pointer variable.
book& a = b;
meaning of above statement is:
variable "a" referencing same memory of variable "b". so if value of "b" changes it's automatically reflect to "a" and vice versa.
book *a=&b;
means variable "a" is pointer variable it will store address of "b".
Difference for reference variable memory will be same for both variable, but in pointers memory for both variable will be different.
Related
This question already has answers here:
Is there any way to find the address of a reference?
(10 answers)
Closed 2 years ago.
I am working with pointers and reference variable in C++.
int i=43;
int &refi=i;
What I know is the variable refi is bound to variable i. However it does not have its own memory. It is just a another name to the variable i. Then how can a pointer point to such unstored variable.
#include<iostream>
int main(){
int i=43;
int &refi=i;
int *p=&refi;
std::cout<<p;
return 0;
}
However I am not getting any error for the above code. Instead I am getting address of it.Am I wrong with the concept of reference variable here?If yes how?
The output is
0x61ff04
The reference has the same address in memory as the variable it's referencing. That essentially makes it an alias. Thus, when you take the address of a reference, you get the address of the original variable.
This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 2 years ago.
First of all
I know the differences between & and * for variables but not for the objects of a class
consider I have a class let's call it a "Math"
Math math; // a simple object called math
Math* mathPtr = &math; // this will holds the address of math object
Math& mathRef = math ; // this one is exactly equivalent to the previous one
What are the differences ?!
A very big difference between pointers(*) and references(&) in nearly every situation is that a pointer is an indepedent variable and can be assigned NEW address values; whereas a reference, once assigned, can never refer to any new object until the variable goes out of scope.
Moreover a pointer can be of a null value, and a reference dosen't.
This question already has answers here:
In C, what does a variable declaration with two asterisks (**) mean?
(5 answers)
Closed 7 years ago.
I know *ptr is a pointer variable called ptr
Does **ptr mean its a pointer to a pointer?
If that is true, what is the meaning of a pointer to a pointer?
Any pointers (pun intended) are very much appreciated.
It depends on the context.
In a declaration, X **ptr declares an object that is a pointer to a pointer to X.
Outside of a declaration, **ptr dereferences both pointers, yielding the value.
Remember that a pointer is just an object that holds the address of another object. So a pointer to pointer is just that: an object that holds the address of another object, which happens to be a pointer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
Pointer vs. Reference
I recently started to 'relearn' c++ and encountered a simple question that i always had.
Int *intp = new int(10);
Int& intref = *intp;
intref prints as 10
*intp does so too.
Also do the prints of &intref and intp equal.
Long story short. Is the difference between & and * simply the way you access the value and adress?
Or is there a major difference in usage?
The * notation means what's being pass on the stack is a pointer, ie, address of something. The & says it's a reference.
Refer this Thread
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Difference between pointer variable and reference variable in C++
I saw this simple code the other day, and I consider myself a beginner with pointers, although I have about a year and half experience with c++. Anyways...
Whats the difference between
int a = 0;
int &b = a;
and
int a = 0
int *p = &a;
Obviously, p holds the address of a, but b is a reference to a, meaning I can change the value of a with b. But I can also do the same thing with p. So what are advantages or disadvantages?
A reference must always be initialized
A reference can't be null
Once initialized, a reference can't be changed to be an alias of a different object.
I think it comes down to how you plan to use the variables afterwards in your program. Both statements appear to do the same thing (in this limited scope).
The first approach seems to be (in my opinion) poor programming practice since it may not be obvious later in the program that changing the value of b also changes the value of a. In the second, at least it is known that p is a pointer, so you should expect the side effects that come with changing the value to which it points.