pointer problem in C++

B

Bob Keith

Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.

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

Many thanks.
 
J

Jakob Bieling

Bob Keith said:
Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.

int *a
int* a

Both are the same. 'a' is a pointer to an int.

'a' is a pointer to a pointer to an int.
int*& a
int *&a

Again, those two are the same. 'a' is a reference to a pointer to an
int.

And here 'a' is just a reference to an int.

hth
 
H

Howard

Bob Keith said:
Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.

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

Many thanks.

Didn't you read the replies to your post last week?
 
R

Ron Samuel Klatchko

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

Trying to read between the lines here, I'm guessing there are two
things that are confusing you.

First, to knock off the easy one, let's talk about whitespace. In
general, where whitespace goes is irrelevant (although not always).
So there is no difference between "int *a" and "int* a". Some people
prefer the former because of the rules of declaring multiples
variables (and int* x,y declares x as a pointer to an integer and y as
an integer, but the placement of the * implies otherwise) while others
prefer the latter because the * modifies the type. Just pick one.
When I critique people, I don't care which one they use as long as
they are consistent.

The second issue that looks to be confusing you is references and
pointers. That is normal because the two are quite similiar and all
the implementations I know (which admittedly is not huge) implement
references via pointers.

Both pointers and references can "reference" another variable. But
they do have some differences.

1) Pointers use 'special' syntax while references use standard object
syntax. To access a field of a pointer, you use the -> operator you
use the . operator to access a field of a reference.

2) References must always be bound to a variable while pointers can
legally point to nothing.

My general recommendation is to start by only using references as
formal parameters to a function. Don't bother trying to fully
understand references until that feels comfortable.

samuel
 
D

David White

Ron Samuel Klatchko said:
(e-mail address removed) (Bob Keith) wrote in message

Trying to read between the lines here, I'm guessing there are two
things that are confusing you.

First, to knock off the easy one, let's talk about whitespace. In
general, where whitespace goes is irrelevant (although not always).
So there is no difference between "int *a" and "int* a". Some people
prefer the former because of the rules of declaring multiples
variables (and int* x,y declares x as a pointer to an integer and y as
an integer, but the placement of the * implies otherwise)

Rather than the rules of declaring multiple variables, it is likely to be
because of the rules of the language. To the compiler, the * is bound to the
identifier, not the type. The "rules of declaring of multiple variables" are
simply a consequence of that. If you write "int* x,y" the compiler sees the
type as 'int' and the ints being declared as *x and y.

To the OP, the declaration rules are designed to mimic expressions.

int k;
This says that where 'k' appears in an expression, its type is 'int', e.g.,
k = 1;
k = k + 3;

int *pk = &k;
This says that where *pk appears in an expression, its type is also 'int'.
The * is the dereference-pointer operator. Therefore 'pk' is a pointer to an
int, and *pk is the int that 'pk' points to, in this case 'k'.

*pk = *pk + 1;

Since *pk is an int, this expression takes the int at *pk, adds 1 to it, and
stores it back to *pk.

Some people think it's silly to think of "int *pk" as "declare *pk to be of
type int", even though that's what it was designed to mean. Those people
prefer to think "declare pk to be of type pointer-to-int". That's why they
write "int* pk".

Note that it is only an accident of history that the * was placed on the
left of the identifier and not the right, which enables you to move the *
across the whitespace to the type name. There are many other declarations in
which you can't do that, e.g.,

int a[10];
Array of 10 ints. (You can't have "int[10] a;")

int (*pf)();
Pointer to a function returning an int (i.e., where (*pf)() appears in an
expression its type is 'int')

int (*pa)[10];
Pointer to an array of 10 ints (i.e., where (*pa) appears in an
expression, its type is 'int')

Make your own choice.

David
 
S

Stuart Golodetz

Bob Keith said:
Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.

Perhaps you would be less confused if you'd read the answer I gave to your
post less than a week ago which bore a remarkable resemblance to this one.
Please read the replies you get this time.

a is a pointer to an int

a is a pointer to an int

a is a pointer to a pointer to an int

a is a reference to a pointer to an int

a is a reference to a pointer to an int

a is a reference to an int
Many thanks.

You're welcome... :) And I recommend the following books, since asking for
book recommendations was implicit in your post:

The C++ Programming Language (Stroustrup)
C++ Primer (Lippmann and Lajoie)
Accelerated C++ (Koenig and Moo)

HTH,

Stuart.
 
J

Jakob Bieling

David White said:
Ron Samuel Klatchko said:
(e-mail address removed) (Bob Keith) wrote in message

Trying to read between the lines here, I'm guessing there are two
things that are confusing you.

First, to knock off the easy one, let's talk about whitespace. In
general, where whitespace goes is irrelevant (although not always).
So there is no difference between "int *a" and "int* a". Some people
prefer the former because of the rules of declaring multiples
variables (and int* x,y declares x as a pointer to an integer and y as
an integer, but the placement of the * implies otherwise)

Rather than the rules of declaring multiple variables, it is likely to be
because of the rules of the language. To the compiler, the * is bound to the
identifier, not the type. The "rules of declaring of multiple variables" are
simply a consequence of that. If you write "int* x,y" the compiler sees the
type as 'int' and the ints being declared as *x and y.

To the OP, the declaration rules are designed to mimic expressions.

int k;
This says that where 'k' appears in an expression, its type is 'int', e.g.,
k = 1;
k = k + 3;

int *pk = &k;
This says that where *pk appears in an expression, its type is also 'int'.
The * is the dereference-pointer operator. Therefore 'pk' is a pointer to an
int, and *pk is the int that 'pk' points to, in this case 'k'.

*pk = *pk + 1;

Since *pk is an int, this expression takes the int at *pk, adds 1 to it, and
stores it back to *pk.

Some people think it's silly to think of "int *pk" as "declare *pk to be of
type int", even though that's what it was designed to mean. Those people
prefer to think "declare pk to be of type pointer-to-int". That's why they
write "int* pk".

Note that it is only an accident of history that the * was placed on the
left of the identifier and not the right, which enables you to move the *
across the whitespace to the type name. There are many other declarations in
which you can't do that, e.g.,

int a[10];
Array of 10 ints. (You can't have "int[10] a;")

int (*pf)();
Pointer to a function returning an int (i.e., where (*pf)() appears in an
expression its type is 'int')

int (*pa)[10];
Pointer to an array of 10 ints (i.e., where (*pa) appears in an
expression, its type is 'int')

Make your own choice.


http://www.research.att.com/~bs/bs_faq2.html#whitespace

regards
 
D

David White

Jakob Bieling said:

Thanks. Interesting, but I don't entirely agree with Stroustrup. I have
serious doubts that C and C++ programmers really see pointer declarations
differently. I usually think of 'p' in "int *p" as a "pointer to int" as
well, and I came to C++ from C. The only time I really think of it as "*p is
an int" is where *p appears in an expression and when I explain the syntax
rules in places like this. But that has no influence on how I write
declarations, simply because to write "int* p" is a special case. You simply
can't declare a pointer to a function, for example, in such a way that you
can have the type information in one place and the identifier somewhere
else, with whitespace in between. I hate special cases. I prefer to follow
the rules that the compiler follows, not fight against them and be
inconsistent in the process.

I also suspect that having an "int *p" camp and an "int* p" camp confuses
the hell out of people trying to learn the language. I don't believe that
you explain the declaration syntax rules well enough for a student to
understand any declaration without the "int* p" style appearing to
contradict the words used to explain the rules.

David
 
G

Gavin Deane

David White said:
Thanks. Interesting, but I don't entirely agree with Stroustrup. I have
serious doubts that C and C++ programmers really see pointer declarations
differently. I usually think of 'p' in "int *p" as a "pointer to int" as
well, and I came to C++ from C. The only time I really think of it as "*p is
an int" is where *p appears in an expression and when I explain the syntax
rules in places like this. But that has no influence on how I write
declarations, simply because to write "int* p" is a special case. You simply
can't declare a pointer to a function, for example, in such a way that you
can have the type information in one place and the identifier somewhere
else, with whitespace in between. I hate special cases. I prefer to follow
the rules that the compiler follows, not fight against them and be
inconsistent in the process.

I also suspect that having an "int *p" camp and an "int* p" camp confuses
the hell out of people trying to learn the language. I don't believe that
you explain the declaration syntax rules well enough for a student to
understand any declaration without the "int* p" style appearing to
contradict the words used to explain the rules.

"int *p" confused the hell out of me until I realised I could write it
as "int* p". Just the way I think about things I suppose. Admittedly,
I learnt these things as they came up, rather than someone formally
explaining the full set of declaration syntax rules in one go.

I understand your point about "int* p" being a special case that
cannot be applied to function pointers. But when I later encountered
function pointers, I found that there was so much more syntax going on
in the declarations that the inconsistency in the position of the *
was the least of my worries. I could cope with that, it was all the
parentheses all over the place that bothered me :)

I would still be much happier if I could completely separate type from
variable name, something like

void (*)(int) pf;

instead of

void (*pf)(int);

At the end of the article, Stroustrup says "Whenever something can be
done in two ways, someone will be confused. Whenever something is a
matter of taste, discussions can drag on forever." Sounds to me like a
convincing argument for not having things that can be done two ways as
a matter of taste.

GJD
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top