Ron Samuel Klatchko <mo*********@yahoo.com> wrote in message
news:a7**************************@posting.google.c om...
st*****************@yahoo.com (Bob Keith) wrote in message
news:<b7*************************@posting.google.c om>...
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)
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)[i] appears in an
expression, its type is 'int')
Make your own choice.
David