理解C的复杂声明文档格式.docx
《理解C的复杂声明文档格式.docx》由会员分享,可在线阅读,更多相关《理解C的复杂声明文档格式.docx(7页珍藏版)》请在冰豆网上搜索。
int*
p,q;
第一眼看去,好像是p和q都是int*类型的,但事实上,只有p是一个指针,而q是一个最简单的int型变量。
我们还是继续我们前面的话题,再来看一个指针的指针的例子:
char**argv;
理论上,对于指针的级数没有限制,你可以定义一个浮点类型变量的指针的指针的指针的指针...
再来看如下的声明:
intRollNum[30][4];
int(*p)[4]=RollNum;
int*q[5];
这里,p被声明为一个指向一个4元素(int类型)数组的指针,而q被声明为一个包含5个元素(int类型的指针)的数组。
另外,我们还可以在同一个声明中混合实用*和&
,如下:
int**p1;
//p1isapointer
toapointer
toanint.
int*&
p2;
//p2isareferencetoapointer
int&
*p3;
//ERROR:
Pointer
toareferenceisillegal.
&
p4;
Referencetoareferenceisillegal.
注:
p1是一个int类型的指针的指针;
p2是一个int类型的指针的引用;
p3是一个int类型引用的指针(不合法!
);
p4是一个int类型引用的引用(不合法!
)。
const修饰符
当你想阻止一个变量被改变,可能会用到const关键字。
在你给一个变量加上const修饰符的同时,通常需要对它进行初始化,因为以后的任何时候你将没有机会再去改变它。
例如:
constintn=5;
intconstm=10;
上述两个变量n和m其实是同一种类型的--都是constint(整形恒量)。
因为C++标准规定,const关键字放在类型或变量名之前等价的。
我个人更喜欢第一种声明方式,因为它更突出了const修饰符的作用。
当const与指针一起使用时,容易让人感到迷惑。
例如,我们来看一下下面的p和q的声明:
constint*p;
intconst*q;
他们当中哪一个代表constint类型的指针(const直接修饰int),哪一个代表int类型的const指针(const直接修饰指针)?
实际上,p和q都被声明为constint类型的指针。
而int类型的const指针应该这样声明:
int*constr=&
n;
//nhasbeendeclaredasanint
这里,p和q都是指向constint类型的指针,也就是说,你在以后的程序里不能改变*p的值。
而r是一个const指针,它在声明的时候被初始化指向变量n(即r=&
)之后,r的值将不再允许被改变(但*r的值可以改变)。
组合上述两种const修饰的情况,我们来声明一个指向constint类型的const指针,如下:
constint*constp=&
n//nhasbeendeclaredasconstint
下面给出的一些关于const的声明,将帮助你彻底理清const的用法。
不过请注意,下面的一些声明是不能被编译通过的,因为他们需要在声明的同时进行初始化。
为了简洁起见,我忽略了初始化部分;
因为加入初始化代码的话,下面每个声明都将增加两行代码。
char**p1;
//
pointerto
char
constchar**p2;
pointertoconstchar
char*const*p3;
pointertoconstpointerto
constchar*const*p4;
pointertoconstpointertoconstchar
char**constp5;
//constpointerto
constchar**constp6;
char*const*constp7;
//constpointertoconstpointerto
constchar*const*constp8;
//constpointertoconstpointertoconstchar
p1是指向char类型的指针的指针;
p2是指向constchar类型的指针的指针;
p3是指向char类型的const指针;
p4是指向constchar类型的const指针;
p5是指向char类型的指针的const指针;
p6是指向constchar类型的指针的const指针;
p7是指向char类型const指针的const指针;
p8是指向constchar类型的const指针的const指针。
typedef的妙用
typedef给你一种方式来克服“*只适合于变量而不适合于类型”的弊端。
你可以如下使用typedef:
typedefchar*PCHAR;
PCHARp,q;
这里的p和q都被声明为指针。
(如果不使用typedef,q将被声明为一个char变量,这跟我们的第一眼感觉不太一致!
)下面有一些使用typedef的声明,并且给出了解释:
typedefchar*a;
//aisapointertoachar
typedefab();
//bisafunctionthatreturns
//apointertoachar
typedefb*c;
//cisapointertoafunction
//thatreturnsapointertoachar
typedefcd();
//disafunctionreturning
//apointertoafunction
typedefd*e;
//eisapointertoafunction
//returningapointertoa
//functionthatreturnsa
//pointertoachar
evar[10];
//varisanarrayof10pointersto
//functionsreturningpointersto
//functionsreturningpointerstochars.
typedef经常用在一个结构声明之前,如下。
这样,当创建结构变量的时候,允许你不使用关键字struct(在C中,创建结构变量时要求使用struct关键字,如structtagPOINTa;
而在C++中,struct可以忽略,如tagPOINTb)。
typedefstructtagPOINT
{
intx;
inty;
}POINT;
POINTp;
/*ValidCcode*/
函数指针
函数指针可能是最容易引起理解上的困惑的声明。
函数指针在DOS时代写TSR程序时用得最多;
在Win32和X-Windows时代,他们被用在需要回调函数的场合。
当然,还有其它很多地方需要用到函数指针:
虚函数表,STL中的一些模板,WinNT/2K/XP系统服务等。
让我们来看一个函数指针的简单例子:
int(*p)(char);
这里p被声明为一个函数指针,这个函数带一个char类型的参数,并且有一个int类型的返回值。
另外,带有两个float类型参数、返回值是char类型的指针的指针的函数指针可以声明如下:
char**(*p)(float,float);
那么,带两个char类型的const指针参数、无返回值的函数指针又该如何声明呢?
参考如下:
void*(*a[5])(char*const,char*const);
“右左法则”[重要!
!
]
Theright-leftrule:
Startreadingthedeclarationfromtheinnermostparentheses,goright,andthengoleft.Whenyouencounterparentheses,thedirectionshouldbereversed.Onceeverythingintheparentheseshasbeenparsed,jumpoutofit.Continuetillthewholedeclarationhasbeenparsed.
这是一个简单的法则,但能让你准确理解所有的声明。
这个法则运用如下:
从最内部的括号开始阅读声明,向右看,然后向左看。
当你碰到一个括号时就调转阅读的方向。
括号内的所有内容都分析完毕就跳出括号的范围。
这样继续,直到整个声明都被分析完毕。
对上述“右左法则”做一个小小的修正:
当你第一次开始阅读声明的时候,你必须从变量名开始,而不是从最内部的括号。
下面结合例子来演示一下“右左法则”的使用。
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())[])();
//bisafunctionthatreturnsa
//pointertoanarrayofpointers
//tofunctionsreturningfloats.
void*(*c)(char,int(*)());
//cisapointertoafunctionthattakes
//twoparameters:
acharandapointertoa
functionthattakesno
parametersandreturns
anint
//andreturnsapointertovoid.
void**(*d)(int&
char**(*)(char*,char**));
//disapointertoafunctionthattakes
areferencetoanintandapointer
toafunctionthattakestwoparameters:
apointertoacharandapointer
toapointertoachar
andreturnsapointertoapointer
toachar
//andreturnsapointertoapointertovoid
float(*(*e[10])
(int&
))[5];
//eisanarrayof10pointersto
//functionsthattakeasingle
//referencetoanintasanargument
//andreturnpointersto
//anarrayof5floats.