【问题标题】:char const *const *const varNamechar const *const *const varName
【发布时间】:2015-12-09 09:12:58
【问题描述】:

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

size_t advanceToNextRuleEntryRelatedIndex( size_t index, size_t nStrings, char const *const *const strings)

我该如何解释:char const *const *const strings?。

谢谢, 帕万。

【问题讨论】:

  • 从右到左,strings是一个指向const字符集的常量指针?
  • 你研究过 C 声明是如何形成的吗?
  • @Pavan 你应该从这里开始。 stackoverflow.com/questions/4949254/…
  • 酷。根据 Scott Meyers 的说法,const 越多,代码就越专业。根据我的口味,缺少一个 const - 在 char 之前。可能是:'const char const *const *const strings' :-)

标签: c pointers


【解决方案1】:
char const *const *const strings
 ^    v    ^  v   ^  v
 |    |    |  |   |  |
 +-----    +--+   +--+

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

例如

char* p{"string1","string2"};

会衰减成char**

当传递给

int n = 0; 
advanceToNextRuleEntryRelatedIndex( n, 2, p);

【讨论】:

  • const 指的是 const 之后的声明,而不是之前的声明。
  • @ValentinHeinitz 这是不正确的。 const 指的是左边的类型。只有在const 左侧没有任何内容的特殊情况下,const 才会引​​用右侧的类型。 int const *p 是指向常量整数的非常量指针。 const int *p 是一样的。 const int const *p 有一个冗余的constint * const p 是一个指向非常量整数的常量指针。见C & C++ conventions for const。当const intint const 都有效时,很容易误解const
  • @sendaran 是的,出于这个原因,我尽可能停止在左侧使用 const,我发现这种方式更加一致,并且可以与 typedefs 配合使用
【解决方案2】:

char const *const *const strings 中,strings 是指向char 指针的指针。如果没有 const 限定符,它看起来像这样:

char **strings;

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

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

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

【讨论】:

    【解决方案3】:

    关键字 const 使该关键字之后的声明成为一个常量。 代码比文字解释得更好:

    /////// 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
    

    【讨论】:

    • 其实我想取消这个答案,但我现在不能,因为 5 分钟过去了。这表示“在这个关键字之后”,而实际上它是“之前”。甚至这个例子也说明了这一点:
    • // 既不能改变单词中的字母,也不能替换单词,而是重新指向其他单词块 const char * const* string2 = (const char * const*) &data[0];
    猜你喜欢
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 2013-04-23
    相关资源
    最近更新 更多