2015-12-09 83 views
3

我有一個具有以下簽名的方法:字符常量* const的* const的varName的

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

我該如何解讀這樣的:char const *const *const strings

謝謝, Pavan。

+3

由右至左,字符串是一個常量指針指向一個恆定指針指向常量字符集? – Arunmu

+1

您是否完成了關於C語言聲明如何形成的研究? – Angew

+2

@Pavan你應該從這個開始。 http://stackoverflow.com/questions/4949254/const-char-const-versus-const-char – Arunmu

回答

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說指向指針是不可變的,第一個指出指向的字符是不可變的。

1

關鍵字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 
+0

其實我想解答這個問題,但是現在我不能'了,因爲5分鐘過去了。這說「在這個關鍵詞之後」實際上是「之前」。即使這個例子說: – Roman

+0

//既不能改變字母中的字母,也不能替換字,但可以重新指向其他字組。 – Roman

2
char const *const *const strings 
^ v ^v ^v 
| | | | | | 
+----- +--+ +--+ 

所以基本上就意味着所有的指針和指針指向是常量字符串,這意味着該函數不能修改傳遞的字符串以任何方式(除非它被鑄造)。

例如

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

其中將衰減成char **

傳遞時

​​
+0

const指const後面的聲明,而不是前面的聲明。 –

+2

@ValentinHeinitz這是不正確的。 'const'指左邊的類型。只有在'const'左邊沒有任何特殊情況時,'const'纔會指向右邊的類型。 'int const * p'是一個非常量指針,指向一個常量整數。 'const int * p'是相同的。 'const int const * p'有一個冗餘'const'。 'int * const p'是一個指向非常量整數的常量指針。請參閱[C&C++常量](https://en.wikipedia.org/wiki/Const_%28computer_programming%29)。當const int和int const都有效時,很容易誤解const。 – sendaran

+0

@sendaran是的,出於這個原因,我儘可能在左側停止使用const,我發現它更一致,並且可以使用typedefs –