这个应该被理解为“declare n as an int”(n是一个int型的变量)。接下去来看一下指针变量,如下:
int *p;
这个应该被理解为“declare p as an int *”(p是一个int *型的变量),或者说p是一个指向一个int型变量的指针。我想在这里展开讨论一下:我觉得在声明一个指针(或引用)类型的变量时,最好将*(或&)写在紧靠变量之前,而不是紧跟基本类型之后。这样可以避免一些理解上的误区,比如: 再来看一个指针的指针的例子:
int **p1;
// p1 is a pointer to a pointer to an int.
int *&p2;
// p2 is a reference to a pointer to an int.
int &*p3;
// ERROR: Pointer to a reference is illegal.
int &&p4;
// ERROR: Reference to a reference is illegal.
typedef char * a;
// a is a pointer to a char
typedef a b();
// b is a function that returns
// a pointer to a char
typedef b *c;
// c is a pointer to a function
// that returns a pointer to a char
typedef c d();
// d is a function returning
// a pointer to a function
// that returns a pointer to a char
typedef d *e;
// e is a pointer to a function
// returning a pointer to a
// function that returns a
// pointer to a char
e var[10];
// var is an array of 10 pointers to
// functions returning pointers to
// functions returning pointers to chars.
float ( * ( *b()) [] )();
// b is a function that returns a
// pointer to an array of pointers
// to functions returning floats.
void * ( *c) ( char, int (*)());
// c is a pointer to a function that takes
// two parameters:
// a char and a pointer to a
// function that takes no
// parameters and returns
// an int
// and returns a pointer to void.
void ** (*d) (int &,
char **(*)(char *, char **));
// d is a pointer to a function that takes
// two parameters:
// a reference to an int and a pointer
// to a function that takes two parameters:
// a pointer to a char and a pointer
// to a pointer to a char
// and returns a pointer to a pointer
// to a char
// and returns a pointer to a pointer to void
float ( * ( * e[10])
(int &) ) [5];
// e is an array of 10 pointers to
// functions that take a single
// reference to an int as an argument
// and return pointers to
// an array of 5 floats.