test_cpp.c
#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;
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;
^~~~~
#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
原文:https://blog.51cto.com/iamokay/2426525