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
Related
This question already has answers here:
What does it mean to return a reference?
(4 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 3 months ago.
so I'm currently learning cpp (for fun at my own pace), specifically functions.
and i got a little confused, this is kinda beginner stuff
and it sometimes takes a little more time for me to understand..
i'm having a little hard time every time it comes to messing around with pointers and references
as things get more complicated the more i learn cpp.
i got this function, that confused me a bit:
int &get(int *arry, int index) { return arry[index]; }
int &get is a function of type int reference.
so i must return a reference.. (&)
does this means i'm returning a pointer ? but not dereferencing it ?
so the fact that i'm not dereferencing 'arry' means that i'm returning
the address of arry[index] ?
if so than address of the the reference operator are not the same
i may need a little order here.
thanks in advance.
This question already has answers here:
What happens after C++ references are compiled?
(2 answers)
How references are internally stored in c++? [duplicate]
(3 answers)
Closed 1 year ago.
When we declare a reference variables int & x = something, is the & just a syntactical shorthand in the source code for referencing the value of the variable something? What I mean is, does the compiler insert, under the hood, the necessary calls for pointers in order to access the value of something, or does it use some more direct approach? E.g. do we have that int x & = something is equal to int* x = &something, and using the value at x like x = 10 results in the code *x = 10?
This question already has answers here:
Can I directly assign an address to a pointer? If so, how to do that?
(2 answers)
Closed 2 years ago.
i want to Convert value int (For example: 0x00AAFAD8) to pointer (Also the pointer itself is the 0x00AAFAD8 value). I don't know how it's called, so i want help for manage memory spaces by values like this.
Thanks.
You can set the pointer address this way:
int i = 0x00AAFAD8;
int* ptr = reinterpret_cast<int*>(i);
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:
c++ passing arguments by reference and pointer
(5 answers)
Closed 4 years ago.
Is there any performance difference between passing a pointer and passing a variable by reference? I'm assuming internally they're both using pointers but was wondering if there are any minor differences.
E.g
int v = 5;
by pointer
void MyFunc(int* P);
MyFunc(&v);
or by reference
void MyFunc(int& R);
MyFunc(v);
Both methods will have comparable performance.
Pointers and references are syntactically different but they are identical in terms of runtime performance and generated code in most of the time.