首页 > 其他 > 详细

const char **,char *const *,char ** const

时间:2019-08-04 22:19:19      阅读:142      评论:0      收藏:0      [点我收藏+]
1、告警

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;
                     ^~~~~

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

原文:https://blog.51cto.com/iamokay/2426525

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!