pointer in C++

B

Bob Keith

I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a
int *a
int &a
int& a
int*& a

Many thanks.
 
S

Stuart Golodetz

Bob Keith said:
I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a // a is a pointer to an int
int *a // a is again a pointer to an int
int &a // a is a reference to an int
int& a // a is again a reference to an int
int*& a // a is a reference to a pointer to an int
HTH,

Stuart.

Many thanks.
 
V

Victor Bazarov

Bob Keith said:
I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a
int *a

The two declarations above are the same and declare 'a'
a pointer to an int.
int &a
int& a

The two declarations above are the same and declare 'a'
a reference to an int (no pointers here).

This declaration declares 'a' a reference to a pointer to
an int.

All three last declarations require initialisation if they
are also definitions. Sounds convoluted? It is, sort of.
If your declaration is in the namespace scope or in a function
scope, they are also definitions. And any definition of
a reference requires an initialiser.

int *& rpa; // error - lacks initialiser (namespace scope)
int main()
{
int& a; // error - lacks initialiser (function scope)
}
Many thanks.

Many welcomes.

Victor
 
R

Ron Natalie

Bob Keith said:
I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a
int *a
int &a
int& a
int*& a
Half of those aren't pointers.
The arrangement of the white space is not significant.
 
S

Sin

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
Sure


int* a
int *a

Same exact thing. Should it be on a, or on int? It doensn't change a thing
as far as the compiler is concerned and for clarity there are arguments on
both sides. The important thing to know is that "int *a, b;" will declare a
int pointer (a) and an integer (b), rather than two pointers...

As for what a pointer is, it's simply a placeholder for an address...

Ex:

int v= 12;
int *a= &v;
int *b= &v
int *c= &v;

*a= 13;
// At this point, v == 13, *a == 13, *b == 13 and *c == 13

int &a
int& a

Again, both are the same. They are NOT pointers though they are references.
A reference is similar to a pointer, but it's actually just a variable that
shares the same address as the object it's initialized with. It makes their
use easier for the non initiated, I guess, but it lacks the possibility of
being set to nothing (NULL).

Ex:

int v= 12;
int &a= v;
int &b= v;
int &c= v;

a= 13;
// At this point, v == 13, a == 13, b == 13 and c == 13


That would be a reference to a pointer...

Ex:

int v= 12;
int &r= v;
int *p= &r;
int *&rp= p;

*rp= 13;

// At this point, v == 13, r == 13, *p == 13, **rp == 13


In my opinion references should be avoided unless necessary. I mostly do C
with occasionnal C++ and I can count on the fingers of one hand the times I
had to use references in the past 5 years...

Alex.
 
R

Ron Natalie

Howard said:
Half? So, say, 2.5 of those 5 variables are (or are not) pointers? Perhaps
this is new math? :)

Sure, the reference to a pointer counts as half :)
 
J

Jakob Bieling

The declaration of 'int *& a;' will get an error from the compiler.

'int*& a;' will give you an error yes, because you are not initializing
the reference. Just like 'int& a;' will give you an error. The following is
valid:

int* b = 0;
int*& a = b;

regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top