指针
在C编程语言中,*p表示指针中存储的值,p表示值的地址,被称为指针。
const char*和char const*表示指针可以指向一个常量字符,指针指向的字符的值不能被改变。但是我们可以改变指针的值,因为它不是常量,可以指向另一个常量字符。
char* const表示指针可以指向一个字符,指针指向的字符的值可以被改变。但是我们不能改变指针的值,因为它现在是常量,不能指向另一个字符。
const char* const表示指针可以指向一个常量字符,指针指向的字符的值不能被改变。我们也不能改变指针的值,因为它现在是常量,不能指向另一个常量字符。
命名语法的原则是从右到左。
// constant pointer to constant charconst char * const// constant pointer to charchar * const// pointer to constant charconst char *
登录后复制
示例(C)
取消注释错误的代码并查看错误。
实时演示
#include 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", *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;}
登录后复制
输出
CE
登录后复制
以上就是C中const char*p、char*const p和const char*const p之间的差异的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2582885.html