首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >char const *const *const varName

char const *const *const varName
EN

Stack Overflow用户
提问于 2015-12-09 17:12:58
回答 3查看 3.9K关注 0票数 3

我有一个具有以下签名的方法:

代码语言:javascript
复制
size_t advanceToNextRuleEntryRelatedIndex( size_t index, size_t nStrings, char const *const *const strings)

我该如何解释这个:char const *const *const strings

谢谢,帕万。

EN

回答 3

Stack Overflow用户

发布于 2015-12-09 19:26:28

代码语言:javascript
复制
char const *const *const strings
 ^    v    ^  v   ^  v
 |    |    |  |   |  |
 +-----    +--+   +--+

所以基本上它意味着所有的指针和指针所指向的字符串都是常量,这意味着函数不能以任何方式修改传递的字符串(除非它被强制转换)。

例如:

代码语言:javascript
复制
char* p{"string1","string2"};

它会腐烂成焦炭**

当传递给

代码语言:javascript
复制
int n = 0; 
advanceToNextRuleEntryRelatedIndex( n, 2, p);
票数 4
EN

Stack Overflow用户

发布于 2015-12-09 18:54:09

char const *const *const strings中,strings是指向char指针的指针。如果没有const限定符,它将如下所示:

代码语言:javascript
复制
char **strings;

const限定符禁止在特定的取消引用级别修改取消引用的值:

代码语言:javascript
复制
**strings = (char) something1; // not allowed because of the first const
*strings = (char *) something2; // not allowed because of the second const
strings = (char **) something3; // not allowed because of the third const

换句话说,第三个常量表示指针本身是不可变的,第二个常量表示指向的指针是不可变的,第一个常量表示指向的字符是不可变的。

票数 2
EN

Stack Overflow用户

发布于 2015-12-09 19:18:43

关键字const将此关键字之后的声明转换为一个常量。代码比文字解释得更好:

代码语言:javascript
复制
/////// Test-code. Place anywhere in global space in C/C++ code, step with debugger
char a1[] = "test1";
char a2[] = "test2";

char *data[2] = {a1,a2};

// Nothing const, change letters in words, replace words, re-point to other block of words
char **string = &data[0]; 

// Can't change letters in words, but replace words, re-point to other block of words
const char **string1 = (const char **) &data[0];
// Can neither change letters in words, not replace words, but re-point to other block of words
const char * const* string2 = (const char * const*) &data[0];
// Can change nothing, however I don't understand the meaning of the 2nd const
const char const* const* const string3 = (const char const* const* const ) &data[0];


int foo()
{

    // data in debugger is:               {"test1","test2"}
    **string = 'T';         //data is now {"Test1","test2"}
    //1 **string1 = 'T';    //Compiler error: you cannot assign to a variable that is const (VS2008)
    *string1=a2;            //data is now {"test2","test2"}
    //2 **string2='T';      //Compiler error: you cannot assign to a variable that is const (VS2008)
    //3 *string2=a2;        //Compiler error: you cannot assign to a variable that is const (VS2008)
    string2=string1;

    //4 **string3='T';      //Compiler error: you cannot assign to a variable that is const (VS2008)
    //5 *string3=a2;        //Compiler error: you cannot assign to a variable that is const (VS2008)
    //6 string3=string1;    //Compiler error: you cannot assign to a variable that is const (VS2008)
    return 0;
}

static int dummy = foo();

/////// END OF Test-code
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34174761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档