码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-08-04 22:29:44      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:错误   top   UNC   com   ubuntu   sign   warning   type   har   

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

标签:错误   top   UNC   com   ubuntu   sign   warning   type   har   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!