I have a method that is having the following signature:
size_t advanceToNextRuleEntryRelatedIndex( size_t index, size_t nStrings, char const *const *const strings)
How do I interpret this: char const *const *const strings
?.
Thanks, Pavan.
I have a method that is having the following signature:
size_t advanceToNextRuleEntryRelatedIndex( size_t index, size_t nStrings, char const *const *const strings)
How do I interpret this: char const *const *const strings
?.
Thanks, Pavan.
char const *const *const strings
^ v ^ v ^ v
| | | | | |
+----- +--+ +--+
so basically it means all pointers and the strings that the pointers point to are constant, meaning the function cannot modify the passed strings in any way (except if it gets casted).
e.g.
char* p[] = {"string1","string2"};
which will decay into char**
when passed to
int n = 0;
advanceToNextRuleEntryRelatedIndex( n, 2, p);
In char const *const *const strings
, strings
is a pointer to a char
pointer. Without the const
qualifiers it would look like this:
char **strings;
The const
qualifiers prohibit modifying the dereferenced value at the particular level of dereferencing:
**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
In other words, the third const says that the pointer itself is immutable, the second const says that the pointed-to pointer is immutable and the first says that the pointed-to character is immutable.
The key-word const makes the declaration after this key-word to a constant. The code explains better than words:
/////// 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