typedef用途之一:理解C/C++中复杂声明
2013-08-08 22:05
399 查看
看如下的声明:
int RollNum[30][4];
int (*p)[4]=RollNum;
int *q[5];
这里,p被声明为一个指向一个4元素(int类型)数组的指针,而q被声明为一个包含5个元素(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.
注:p1是一个int类型的指针的指针;p2是一个int类型的指针的引用;p3是一个int类型引用的指针(不合法!);p4是一个int类型引用的引用(不合法!)。
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.
typedef经常用在一个结构声明之前,如下。这样,当创建结构变量的时候,允许你不使用关键字struct(在C中,创建结构变量时要求使用struct关键字,如struct
tagPOINT a;而在C++中,struct可以忽略,如tagPOINT b)。
typedef struct tagPOINT
{
int x;
int y;
}POINT;
POINT p; /* Valid C code */
下面结合例子来演示一下“右左法则”的使用。
int * (* (*fp1) (int) ) [10];
阅读步骤:
1. 从变量名开始 -------------------------------------------- fp1
2. 往右看,什么也没有,碰到了),因此往左看,碰到一个* ------ 一个指针
3. 跳出括号,碰到了(int) ----------------------------------- 一个带一个int参数的函数
4. 向左看,发现一个* --------------------------------------- (函数)返回一个指针
5. 跳出括号,向右看,碰到[10] ------------------------------ 一个10元素的数组
6. 向左看,发现一个* --------------------------------------- 指针
7. 向左看,发现int ----------------------------------------- int类型
总结:fp1被声明成为一个函数的指针,该函数返回指向指针数组的指针.
再来看一个例子:
int *( *( *arr[5])())();
阅读步骤:
1. 从变量名开始 -------------------------------------------- arr
2. 往右看,发现是一个数组 ---------------------------------- 一个5元素的数组
3. 向左看,发现一个* --------------------------------------- 指针
4. 跳出括号,向右看,发现() -------------------------------- 不带参数的函数
5. 向左看,碰到* ------------------------------------------- (函数)返回一个指针
6. 跳出括号,向右发现() ------------------------------------ 不带参数的函数
7. 向左,发现* --------------------------------------------- (函数)返回一个指针
8. 继续向左,发现int --------------------------------------- int类型
// 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.
int RollNum[30][4];
int (*p)[4]=RollNum;
int *q[5];
这里,p被声明为一个指向一个4元素(int类型)数组的指针,而q被声明为一个包含5个元素(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.
注:p1是一个int类型的指针的指针;p2是一个int类型的指针的引用;p3是一个int类型引用的指针(不合法!);p4是一个int类型引用的引用(不合法!)。
typedef的妙用
typedef char * a; // a is a pointer to a chartypedef 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.
typedef经常用在一个结构声明之前,如下。这样,当创建结构变量的时候,允许你不使用关键字struct(在C中,创建结构变量时要求使用struct关键字,如struct
tagPOINT a;而在C++中,struct可以忽略,如tagPOINT b)。
typedef struct tagPOINT
{
int x;
int y;
}POINT;
POINT p; /* Valid C code */
“右左法则“
这是一个简单的法则,但能让你准确理解所有的声明。这个法则运用如下:从变量名开始阅读声明,向右看,然后向左看。当你碰到一个括号时就调转阅读的方向。括号内的所有内容都分析完毕就跳出括号的范围。这样继续,直到整个声明都被分析完毕。下面结合例子来演示一下“右左法则”的使用。
int * (* (*fp1) (int) ) [10];
阅读步骤:
1. 从变量名开始 -------------------------------------------- fp1
2. 往右看,什么也没有,碰到了),因此往左看,碰到一个* ------ 一个指针
3. 跳出括号,碰到了(int) ----------------------------------- 一个带一个int参数的函数
4. 向左看,发现一个* --------------------------------------- (函数)返回一个指针
5. 跳出括号,向右看,碰到[10] ------------------------------ 一个10元素的数组
6. 向左看,发现一个* --------------------------------------- 指针
7. 向左看,发现int ----------------------------------------- int类型
总结:fp1被声明成为一个函数的指针,该函数返回指向指针数组的指针.
再来看一个例子:
int *( *( *arr[5])())();
阅读步骤:
1. 从变量名开始 -------------------------------------------- arr
2. 往右看,发现是一个数组 ---------------------------------- 一个5元素的数组
3. 向左看,发现一个* --------------------------------------- 指针
4. 跳出括号,向右看,发现() -------------------------------- 不带参数的函数
5. 向左看,碰到* ------------------------------------------- (函数)返回一个指针
6. 跳出括号,向右发现() ------------------------------------ 不带参数的函数
7. 向左,发现* --------------------------------------------- (函数)返回一个指针
8. 继续向左,发现int --------------------------------------- int类型
更多的例子:
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.
相关文章推荐
- 理解复杂的C/C++声明 const, typedef , 函数指针
- 理解复杂的C/C++声明 const, typedef , 函数指针
- [zz]理解复杂的C/C++声明 const, typedef , 函数指针(转贴)
- 理解复杂的C/C++声明 const, typedef , 函数指针
- 理解复杂的C/C++声明 const, typedef , 函数指针(转贴)
- 理解复杂的C/C++声明 const, typedef , 函数指针(转贴)
- 转 如何理解c和c++ 的复杂类型声明
- 如何理解C和C++的复杂类型声明
- 如何理解c和c++ 的复杂类型声明
- 如何理解c和c++的复杂类型声明
- 如何理解c和c++的复杂类型声明
- 理解c和c++的复杂类型声明 (掺杂指针)
- 如何理解c和c++的复杂类型声明
- 教你理解复杂的C/C++声明
- c和c++的复杂类型声明理解
- 如何理解c和c++的复杂类型声明
- [转帖]如何理解C和C++的复杂类型声明
- 如何理解c和c++的复杂类型声明
- typedef (包含对复杂函数声明的理解)
- 如何理解c和c++的复杂声明