Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "int * const const * b" mean?

Tags:

c

declaration

int * const const * b

What does it mean? cdecl says declare b as pointer to const const pointer to int. Can someone explain it?

like image 964
bitstore Avatar asked Apr 14 '26 06:04

bitstore


2 Answers

const const is redundant, it will reduce to const.

So let's rewrite that:

int * const *b;

So b is a pointer to a const pointer to an int. With an example:

int main(int argc, char **argv) {
    int i = 5;
    int *ip = &i;
    int * const const * b = &ip;

    (**b)++; /* legal */
    (*b)++; /* illegal */
    (b)++; /* legal */

    return 0;
}
like image 154
orlp Avatar answered Apr 15 '26 19:04

orlp


Also see that question, this is valid C99, but not valid C++ nor valid C89.

It's just a pointer to a const pointer to an integer. Const applies to what is on its left unless there is nothing on its left: then it applies on the right.

Here you just have redundant redundancy, which is repeated such that you're really sure that it is const and will not mutate.

Here you just have redundant redundancy, which is repeated such that you're really sure that it is const and will not mutate.

Even cdecl.org tells that b is pointer to const const pointer to int.

like image 20
Benoit Avatar answered Apr 15 '26 19:04

Benoit