Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clarify and remember const usage in C [closed]

Tags:

c

argv is an array of constant pointers to characters

char * const argv[] // 1

argv is an array of pointers to characters, which are constant

const char * argv[] // 2

Is there a tip to remember number 1?

like image 649
Ankur Agarwal Avatar asked Sep 06 '25 03:09

Ankur Agarwal


1 Answers

const char * argv[] // 2

can also be written:

char const * argv[] // 3

because C doesn't care about the order of const in the type. If you write it this way, then the thing actually const is the thing to the left of the keyword const. The form where const is first is the one exception to that rule; but in that case there's nothing to the left of const so it is easy to avoid that case with this rule of thumb.

char*             // mutable pointer to mutable char
char const*       // mutable pointer to constant char
char * const      // constant pointer to mutable char
char const* const // constant pointer to constant char
like image 89
Billy ONeal Avatar answered Sep 07 '25 22:09

Billy ONeal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!