const char **,char *const *,char ** const
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了const char **,char *const *,char ** const相关的知识,希望对你有一定的参考价值。
1、告警test_cpp.c
#include <stdlib.h>
#include <stdio.h>
int main()
char c = ‘c‘;
char c_q = ‘c‘;
char *p = &c;
char *q = &c_q;
char **pp = &p;
const char **cpp = &p;
char const **c_pp = &p;
char * const *pcp = &p;
char ** const ppc = &p;
return 0;
编译的时候有如下警告:
x@ubuntu:~/Desktop/const$ gcc text_cpp.c
text_cpp.c: In function ‘main’:
text_cpp.c:13:21: warning: initialization of ‘const char **’ from incompatible pointer type ‘char **’ [-Wincompatible-pointer-types]
const char **cpp = &p;
^
text_cpp.c:14:22: warning: initialization of ‘const char **’ from incompatible pointer type ‘char **’ [-Wincompatible-pointer-types]
char const **c_pp = &p;
^
2、错误
#include <stdlib.h>
#include <stdio.h>
int main()
char *p_str[] = "char **",
"const char ** ",
"char * const * ",
"char ** const ",;
char **pp = p_str;
const char **cpp = p_str;
char * const *pcp = p_str;
char ** const ppc = p_str;
*pp = "assign char **";
*cpp = "assign const char **";
*pcp = "assign char * const *";
*ppc = "assign char ** const";
return 0;
出现如下编译错误:
x@ubuntu:~/Desktop/const$ gcc text_cpp.c
text_cpp.c: In function ‘main’:
text_cpp.c:12:21: warning: initialization of ‘const char **’ from incompatible pointer type ‘char **’ [-Wincompatible-pointer-types]
const char **cpp = p_str;
^~~~~
text_cpp.c:18:7: error: assignment of read-only location ‘*pcp’
*pcp = "assign char * const *";
^
以上是关于const char **,char *const *,char ** const的主要内容,如果未能解决你的问题,请参考以下文章
深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**cons(示例
const char **,char *const *,char ** const
const char * char const * char * const 三者的区别
const char *p; char const *p; char * const p的区别