Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

char const *const *const varName

Tags:

c

pointers

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.

like image 363
Pavan Dittakavi Avatar asked Sep 03 '25 02:09

Pavan Dittakavi


2 Answers

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);
like image 69
AndersK Avatar answered Sep 04 '25 16:09

AndersK


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.

like image 37
Blagovest Buyukliev Avatar answered Sep 04 '25 16:09

Blagovest Buyukliev