
在C编程语言中,*p表示指针中存储的值,p表示值的地址,被称为指针。
const char*和char const*表示指针可以指向一个常量字符,指针指向的字符的值不能被改变。但是我们可以改变指针的值,因为它不是常量,可以指向另一个常量字符。
char* const表示指针可以指向一个字符,指针指向的字符的值可以被改变。但是我们不能改变指针的值,因为它现在是常量,不能指向另一个字符。
const char* const表示指针可以指向一个常量字符,指针指向的字符的值不能被改变。我们也不能改变指针的值,因为它现在是常量,不能指向另一个常量字符。
命名语法的原则是从右到左。
// constant pointer to constant char const char * const // constant pointer to char char * const // pointer to constant char const char *
取消注释错误的代码并查看错误。
实时演示
#include <stdio.h>
int main() {
//Example: char const*
//Note: char const* is same as const char*
const char p = 'A';
// q is a pointer to const char
char const* q = &p;
//Invalid asssignment
// value of p cannot be changed
// error: assignment of read-only location '*q'
//*q = 'B';
const char r = 'C';
//q can point to another const char
q = &r;
printf("%c</p><p>", *q);
//Example: char* const
char u = 'D';
char * const t = &u;
//You can change the value
*t = 'E';
printf("%c", *t);
// Invalid asssignment
// t cannot be changed
// error: assignment of read-only variable 't'
//t = &r;
//Example: char const* const
char const* const s = &p;
// Invalid asssignment
// value of s cannot be changed
// error: assignment of read-only location '*s'
// *s = 'D';
// Invalid asssignment
// s cannot be changed
// error: assignment of read-only variable 's'
// s = &r;
return 0;
}C E
以上就是C中const char*p、char*const p和const char*const p之间的差异的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号