C语言中的const关键字的用途:
(1) :定义常变量(只读变量),存储在rodata段。
int const x = 9;
const int x = 9;
注:以上两种定义方法效果一样,即编译器对它们的理解是一样的。
(2) :修饰函数参数,函数返回值,保护变量被意外修改,增强程序的健壮性。
void Strcmp(char *StrDes, const char *StrRes);
const LinkList *CreateLinkListF(LinkList *L, int n) ;
(3)让逻辑错误在编译期间被发现,而不要到了运行阶段程序异常终止。
const char *p="linux";
p[0] = 'L'; //编译时发生错误。
char *p="linux";
p[0] = 'L'; //运行时发生错误,异常终止。