Clarify and remember const usage in C -


argv array of constant pointers characters

char * const argv[] // 1 

argv array of pointers characters, constant

const char * argv[] // 2 

is there tip remember number 1?

char * const argv[] 

there simple rules, cskoala mentioned in answer:

  1. find identifier

    char * const     argv     []                   |               identifier                   1   
  2. read element right of identifier, left-to-right

    char * const     argv       []                   |         |               identifier  array                   1         2 
  3. read element left of identifier, right-to-left

    char     *     const    argv       []   |      |       |        |         | char  pointer  const  identifier  array   5      4       3        1         2 

result: (1) argv (2) array of (3) constants of type (4) pointer (5) char


other examples.

    char  const     *       argv       []       |     |       |         |         |     char  const  pointer  identifier  array       5     4       3         1         2 

result: (1) argv (2) array of (3) pointers (4) constants of type (5) char

    char  const     *     const    argv       []       |     |       |       |        |         |     char  const  pointer  const  identifier  array       6     5       4       3        1         2 

result: (1) argv (2) array of (3) constants of type (4) pointer (5) constant of type (6) char


there notably ugly exception simple rule. reason c allows place const modifier left of type, in example:

    const  char     *       argv       []       |      |      |         |         |     const  char  pointer  identifier  array       4      5      3         1         2 

in opinion it's better avoid such declarations, consistency. it's leads confusion or errors.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

php - Dynamic url re-writing using htaccess -

java - Multi-Label Document Classification -