C:声明一个指向常量字符数组的常量指针

发布于 2024-09-14 21:14:32 字数 902 浏览 4 评论 0原文

我试图理解数组声明、常量及其结果变量类型。

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

      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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情场扛把子 2024-09-21 21:14:32
const char *const s15 = "abc";
const char *const s15 = "abc";
倒数 2024-09-21 21:14:32

您的第一个 typeof 评论并不正确。 s01 的类型为 char [4]s02s03 的类型为 const字符[4]。当在表达式中使用且不是 &sizeof 运算符的主语时,它们将计算为 char *char * 类型的右值code>const char * 分别指向数组的第一个元素。

你不能以这样的方式声明它们:它们会衰减到本身是 const 限定的右值;拥有 const 限定的右值实际上没有任何意义,因为无法分配右值。这就像说您想要一个 const int 类型的 5 常量,而不是 int 类型。

Your first typeof comments aren't really correct. The type of s01 is char [4], and the types of s02 and s03 are const char [4]. When used in an expression and not the subject of either the & or sizeof operators, they will evaluate to rvalues of type char * and const 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 type const int rather than int.

末が日狂欢 2024-09-21 21:14:32

s01 等并不是真正的指针类型,它们是数组类型。从这个意义上说,它们的行为有点像 const 指针(例如,您不能重新分配 s01 来指向其他地方)。

s01 et al are not really pointer types, they're array types. In that sense, they already act a bit like const pointers (you cannot re-assign s01 to point somewhere else, for instance).

夏至、离别 2024-09-21 21:14:32

使用cdecl

cdecl> declare foo as constant pointer to array of constant char
Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
        (maybe you mean "pointer to object")
const char (* const foo)[]
cdecl> declare foo as constant pointer to array 4 of constant char
const char (* const foo)[3]
cdecl> declare foo as constant pointer to constant char
const char * const foo

C中很少使用指向数组的指针;通常 API 函数需要一个指向第一个元素的指针。

Use cdecl:

cdecl> declare foo as constant pointer to array of constant char
Warning: Unsupported in C -- 'Pointer to array of unspecified dimension'
        (maybe you mean "pointer to object")
const char (* const foo)[]
cdecl> declare foo as constant pointer to array 4 of constant char
const char (* const foo)[3]
cdecl> declare foo as constant pointer to constant char
const char * const foo

Pointers to arrays are rarely used in C; usually API functions expect a pointer to the first element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文