C:声明一个指向常量字符数组的常量指针
我试图理解数组声明、常量及其结果变量类型。
以下是允许的(我的编译器):
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*)
或者,我们可以手动声明数组大小:
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*)
如何获取 const char* const
类型的结果变量?以下是不允许的(我的编译器):
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" ;
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的第一个
typeof
评论并不正确。s01
的类型为char [4]
,s02
和s03
的类型为const字符[4]
。当在表达式中使用且不是&
或sizeof
运算符的主语时,它们将计算为char *
和char *
类型的右值code>const char * 分别指向数组的第一个元素。你不能以这样的方式声明它们:它们会衰减到本身是 const 限定的右值;拥有 const 限定的右值实际上没有任何意义,因为无法分配右值。这就像说您想要一个
const int
类型的5
常量,而不是int
类型。Your first
typeof
comments aren't really correct. The type ofs01
ischar [4]
, and the types ofs02
ands03
areconst char [4]
. When used in an expression and not the subject of either the&
orsizeof
operators, they will evaluate to rvalues of typechar *
andconst char *
respectively, pointing at the first element of the array.You can't declare them in such a way that they decay to an rvalue that itself is const-qualified; it doesn't really make any sense to have a const-qualified rvalue, since rvalues can't be assigned to. It's like saying you want a
5
constant that is of typeconst int
rather thanint
.s01
等并不是真正的指针类型,它们是数组类型。从这个意义上说,它们的行为有点像 const 指针(例如,您不能重新分配 s01 来指向其他地方)。s01
et al are not really pointer types, they're array types. In that sense, they already act a bit likeconst
pointers (you cannot re-assigns01
to point somewhere else, for instance).使用cdecl:
C中很少使用指向数组的指针;通常 API 函数需要一个指向第一个元素的指针。
Use cdecl:
Pointers to arrays are rarely used in C; usually API functions expect a pointer to the first element.