C:声明一个常量指针的常量字符数组 [英] C: declare a constant pointer to an array of constant characters

查看:146
本文介绍了C:声明一个常量指针的常量字符数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解数组声明,常量性,以及它们产生的变量类型。

以下是允许的(我的编译器):

  S01的char [] =ABC; // typeof运算(S01)=的char *
为const char S02 [] =ABC; // typeof运算(S02)=为const char *(==字符常量*)
字符常量S03 [] =ABC; // typeof运算(S03)=字符常量*(==为const char *)

另外,我们可以手动声明数组的大小:

 的char S04 [4] =ABC; // typeof运算(S04)=的char *
为const char S05 [4] =ABC; // typeof运算(S05)=为const char *(==字符常量*)
字符常量S06 [4] =ABC; // typeof运算(S06)=字符常量*(==为const char *)

我如何得到一个结果类型的变量为const char * const的?以下是不允许的(我的编译器):

 为const char S07常量[] =ABC;
字符常量S08常量[] =ABC;
为const char S09 [] =常量ABC;
字符常量S10 [] =常量ABC;
为const char S11常量[4] =ABC;
字符常量S12 const的[4] =ABC;
为const char S13 [4]常量=ABC;
字符常量S14 [4]常量=ABC;

感谢


解决方案

 为const char * const的S15 =ABC;

I am trying to understand array declarations, constness, and their resulting variable types.

The following is allowed (by my compiler):

      char s01[] = "abc" ;  // typeof(s01) = char*
const char s02[] = "abc" ;  // typeof(s02) = const char* (== char const*)
char const s03[] = "abc" ;  // typeof(s03) = char const* (== const char*)

Alternatively, we can declare the array size manually:

      char s04[4] = "abc" ;  // typeof(s04) = char*
const char s05[4] = "abc" ;  // typeof(s05) = const char* (== char const*)
char const s06[4] = "abc" ;  // typeof(s06) = char const* (== const char*)

How do I get a resulting variable of type const char* const? The following are not allowed (by my compiler):

const char s07 const[] = "abc" ;
char const s08 const[] = "abc" ;
const char s09[] const = "abc" ;
char const s10[] const = "abc" ;
const char s11 const[4] = "abc" ;
char const s12 const[4] = "abc" ;
const char s13[4] const = "abc" ;
char const s14[4] const = "abc" ;

Thanks

解决方案

const char *const s15 = "abc";

这篇关于C:声明一个常量指针的常量字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆