2

code:

const char * const key;

There are 2 const in above pointer, I saw things like this the first time.

I know the first const makes the value pointed by the pointer immutable, but did the second const make the pointer itself immutable?

Anyone can help to explain this?


@Update:

And I wrote a program that proved the answer is correct.

#include <stdio.h>

void testNoConstPoiner() {
    int i = 10;

    int *pi = &i;
    (*pi)++;
    printf("%d\n", i);
}

void testPreConstPoinerChangePointedValue() {
    int i = 10;

    const int *pi = &i;

    // this line will compile error
    // (*pi)++;
    printf("%d\n", *pi);
}


void testPreConstPoinerChangePointer() {
    int i = 10;
    int j = 20;

    const int *pi = &i;
    pi = &j;
    printf("%d\n", *pi);
}

void testAfterConstPoinerChangePointedValue() {
    int i = 10;

    int * const pi = &i;
    (*pi)++;
    printf("%d\n", *pi);
}

void testAfterConstPoinerChangePointer() {
    int i = 10;
    int j = 20;

    int * const pi = &i;
    // this line will compile error
    // pi = &j
    printf("%d\n", *pi);
}

void testDoublePoiner() {
    int i = 10;
    int j = 20;

    const int * const pi = &i;
    // both of following 2 lines will compile error
    // (*pi)++;
    // pi = &j
    printf("%d\n", *pi);
}

int main(int argc, char * argv[]) {
    testNoConstPoiner();

    testPreConstPoinerChangePointedValue();
    testPreConstPoinerChangePointer();

    testAfterConstPoinerChangePointedValue();
    testAfterConstPoinerChangePointer();

    testDoublePoiner();
}

Uncomment lines in 3 of the functions, will get compile error with tips.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Eric
  • 22,183
  • 20
  • 145
  • 196
  • `const char *` is a pointer to a constant char, while `char * const` is a constant pointer to a char. So `const char * const` means constant pointer to constant char, i think. – Himanshu Nov 22 '14 at 06:32
  • http://cdecl.org/ http://cdecl.ridiculousfish.com/?q=const+char+%2A+const+key%3B – phuclv Nov 22 '14 at 07:00
  • 1
    The trick is that `const` binds to its left, _unless there's nothing there_, in which case it binds to its right. So while we normally write `const int foo;` it would be more consistent and just as correct to write `int const foo;`. Similarly, your original question can be written as `char const * const key;`: a `const` pointer to a `const` `char`. – etheranger Dec 19 '14 at 05:29

2 Answers2

9

First const tells you can not change *key, key[i] etc

Following lines are invalid

*key = 'a';
*(key + 2) = 'b';
key[i] = 'c';

Second const tells that you can not change key

Following lines are invalid

key = newkey;
++key;

Also check how to read this complex declaration


Adding more details.

  1. const char *key: you can change key but can not change the chars pointed by key.
  2. char *const key: You can not change key but can the chars pointed by key
  3. const char *const key: You can not change the key as well as the pointer chars.
Gyapti Jain
  • 4,056
  • 20
  • 40
  • 1
    Thanks, I just wrote a program and the result is exactly the same as your answer. – Eric Nov 22 '14 at 06:54
  • There's a fundamental difference between these two consts: the first `const` is just a constness of an *access path* to some remote object `*key`. It can be legally removed by an explicit cast, thus allowing you to modify `*key` (assuming `*key` itself is modifiable). E.g. `*(char *) key = 'a'` might be legal. The second `const` describes constness of an object `key` itself. It is a "hard" const - it makes `key` immutable and there's no way around it. – AnT stands with Russia Apr 10 '15 at 07:26
0

const [type]* means that it's a pointer that does not change the pointed value. [type]* const means that the value of the pointer itself cannot change, ie, it keeps pointing to the same value, akin to the Java final keyword.

Jules G.M.
  • 3,624
  • 1
  • 21
  • 35