C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

pointer in C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Bob Keith
Guest





PostPosted: Fri Jun 27, 2003 6:48 pm    Post subject: pointer in C++ Reply with quote



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.
Back to top
Stuart Golodetz
Guest





PostPosted: Fri Jun 27, 2003 7:05 pm    Post subject: Re: pointer in C++ Reply with quote



"Bob Keith" <structuralvibration (AT) yahoo (DOT) com> wrote

Quote:
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.

Quote:
Many thanks.



Back to top
Victor Bazarov
Guest





PostPosted: Fri Jun 27, 2003 7:09 pm    Post subject: Re: pointer in C++ Reply with quote



"Bob Keith" <structuralvibration (AT) yahoo (DOT) com> wrote...
Quote:
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.

Quote:
int &a
int& a

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

Quote:
int*& a

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)
}

Quote:

Many thanks.

Many welcomes.

Victor



Back to top
Ron Natalie
Guest





PostPosted: Fri Jun 27, 2003 7:33 pm    Post subject: Re: pointer in C++ Reply with quote


"Bob Keith" <structuralvibration (AT) yahoo (DOT) com> wrote

Quote:
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.



Back to top
Sin
Guest





PostPosted: Fri Jun 27, 2003 7:36 pm    Post subject: Re: pointer in C++ Reply with quote

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

Sure


Quote:
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


Quote:
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


Quote:
int*& a

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.



Back to top
Ron Natalie
Guest





PostPosted: Fri Jun 27, 2003 10:34 pm    Post subject: Re: pointer in C++ Reply with quote


"Howard" <alicebt (AT) hotmail (DOT) com> wrote

Quote:

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote in message
news:KdydnbiyyshyBmGjXTWQlg (AT) giganews (DOT) com...


int* a
int *a
int &a
int& a
int*& a

Half of those aren't pointers.

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

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



Back to top
Jakob Bieling
Guest





PostPosted: Thu Jul 17, 2003 10:51 pm    Post subject: Re: pointer in C++ Reply with quote

"Yuh-Horng Shiau" <yhshiau (AT) mail (DOT) nuu.edu.tw> wrote


Quote:
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
--
jb

(replace y with x if you want to reply by e-mail)



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.