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

时间:2022-12-06 07:40:19

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

我试图理解数组声明,constness及其生成的变量类型。

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 * 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" ;

Thanks

4 个解决方案

#1


9  

const char *const s15 = "abc";

#2


6  

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.

你的第一个类型的评论并不是真的正确。 s01的类型是char [4],s02和s03的类型是const char [4]。当在表达式中使用而不是&或sizeof运算符的主题时,它们将分别计算为char *和const char *类型的rvalues,指向数组的第一个元素。

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.

你不能以这样的方式声明它们,使它们衰变为一个本身符合const的rvalue;具有const限定的右值没有任何意义,因为无法分配rvalues。这就像是说你想要一个类型为const int而不是int的5常量。

#3


5  

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).

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

#4


3  

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.

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

#1


9  

const char *const s15 = "abc";

#2


6  

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.

你的第一个类型的评论并不是真的正确。 s01的类型是char [4],s02和s03的类型是const char [4]。当在表达式中使用而不是&或sizeof运算符的主题时,它们将分别计算为char *和const char *类型的rvalues,指向数组的第一个元素。

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.

你不能以这样的方式声明它们,使它们衰变为一个本身符合const的rvalue;具有const限定的右值没有任何意义,因为无法分配rvalues。这就像是说你想要一个类型为const int而不是int的5常量。

#3


5  

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).

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

#4


3  

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.

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