const int single pointer

In this section, you are going to learn

What is const int a = 5; ?

What is int const a = 5; ?

What is const int *p = &a; ?

What is int const *p = &a; ?

What is int *const p = &a; ?

What is const int *const p = &a; ?

  • Inorder to answer above questions, let us remember a very simple rule

Anything after const keyword CAN NOT be changed

  • Is it that simple ?

    • Yes. Let us see how do we apply above rule to answer the questions

  • Step 1 : Consider the statement

const int a = 5;

:const int a = 5;
        fork
                :const;
        kill
        fork again
                :int;
        kill
        fork again
                :a = 5;
        endfork
        kill

  • Step 2 : Remove all keywords after const

const a = 5;

:const a = 5;
        fork
                :const;
        kill
        fork again
                :a = 5;
        endfork
        kill

  • Step 3 : Remove everything before const

const a = 5;
  • Step 4 : Remove assignment

const a;

:const a;
        fork
                :const;
        kill
        fork again
                :a;
        endfork
        kill

  • Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed

  • We see a after const

  • Means, variable a CAN NOT be changed again in next line

const int a = 5;

a = 10; // --> This is invalid
  • Step 1 : Consider the statement

const int *p = &a;

:const int *p = &a;
        fork
                :const;
        kill
        fork again
                :int;
        kill
        fork again
                : *p = &a;
        endfork
        kill

  • Step 2 : Remove all keywords after const

const *p = &a;

:const *p = &a;
        fork
                :const;
        kill
        fork again
                : *p = &a;
        endfork
        kill

  • Step 3 : Remove everything before const

const *p = &a;
  • Step 4 : Remove assignment

const *p;

:const *p;
        fork
                :const;
        kill
        fork again
                : *p;
        endfork
        kill

  • Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed

  • We see *p after const

  • Means, *p CAN NOT be changed again in next line

int a = 5;

const int *p = &a;

*p = 10; // --> This is invalid
  • Step 6 : Bonus point ! *p and p are different. Means you can change p

    • From Step 4, we derived that “const *p”

    • Means only *p is const and CAN NOT be changed

    • But remember p is entirely different

    • There is nothing which says p is constant

    • Hence p can still be changed

    • See below example

int a = 5, b = 6;

const int *p = &a;

printf("*p is %d\n", *p); // prints 5

p = &b; // --> This is valid

printf("*p is %d\n", *p); // prints 6
  • Step 1 : Consider the statement

int *const p = &a;

:int *const p = &a;;
        fork
                :int *;
        kill
        fork again
                :const;
        kill
        fork again
                : p = &a;
        endfork
        kill

  • Step 2 : Remove all keywords after const

int *const p = &a;
  • Step 3 : Remove everything before const

const p = &a;

:const p = &a;
        fork
                :const;
        kill
        fork again
                : p = &a;
        endfork
        kill

  • Step 4 : Remove assignment

const p;

:const p;
        fork
                :const;
        kill
        fork again
                : p;
        endfork
        kill

  • Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed

  • We see p after const

  • Means, p CAN NOT be changed again in next line

int a = 5, b = 6;

int *const p = &a;

printf("*p is %d\n", *p); // prints 5

p = &b; // --> This is invalid
  • Step 6 : Bonus point ! *p and p are different. Means you can change *p

    • From Step 4, we derived that “const p”

    • Means only p is const and CAN NOT be changed

    • But remember *p is entirely different

    • There is nothing which says *p is constant

    • Hence *p can still be changed

    • See below example

int a = 5, b = 6;

int *const p = &a;

printf("*p is %d\n", *p); // prints 5

*p = 100; // --> This is valid

printf("*p is %d\n", *p); // prints 100
  • There are two occurences of const keyword

  • Hence, let us apply the same rules two times

  • Step 1 : Consider the statement

const int *const p = &a;
  • Step 2 : Remove all keywords after first const

const *p = &a;
  • Step 3 : Remove everything before first const

const *p = &a;
  • Step 4 : Remove assignment

const *p;
  • Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed

*p CAN NOT be changed

  • Step 1 : Consider the statement

const int *const p = &a;
  • Step 2 : Remove all keywords after second const

const int *const p = &a;
  • Step 3 : Remove everything before second const

const p = &a;
  • Step 4 : Remove assignment

const p;
  • Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed

p CAN NOT be changed

Both p and *p CAN NOT be changed

int a = 5, b = 6;

const int *const p = &a;

*p = 100; // --> This is invalid

p = &b;   // --> This is invalid

Statement

Meaning

const int a = 5;

  • a CAN NOT be changed again

int const a = 5;

  • a CAN NOT be changed again

const int *p = &a;

  • *p CAN NOT be changed

  • p can be changed

int const *p = &a;

  • *p CAN NOT be changed

  • p can be changed

int *const p = &a;

  • *p can be changed

  • p CAN NOT be changed

const int *const p = &a;

  • *p CAN NOT be changed

  • p CAN NOT be changed