const char single pointer

In this section, you are going to learn

What is const char a = 5; ?

What is char const a = 5; ?

What is const char *p = &a; ?

What is char const *p = &a; ?

What is char *const p = &a; ?

What is const char *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 char a = 5;
https://www.plantuml.com/plantuml/svg/iqhEpoikKKZEI2nII5ImLJ2rvbA0WhJyecmu1mQimEeHYh8pSt9GT2WafYTcvc7H1xI136reRahDIq5n5iWS00==
  • Step 2 : Remove all keywords after const

const a = 5;
https://www.plantuml.com/plantuml/svg/iqhEpoikKKXKi5KmjUPIW8Aq_A9iE0S6hC1A4OgoCtDoK7Ge9AOdPkPXwaCpF3Kl1SLya3a0
  • Step 3 : Remove everything before const

const a = 5;
  • Step 4 : Remove assignment

const a;
https://www.plantuml.com/plantuml/svg/iqhEpoikKKYqvbA0WhJyecmu1mQimCeGYh8pSt9GT2WafYTcvc7gGp8uDIy5nMoG6G0=
  • 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 char a = 5;

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

const char *p = &a;
https://www.plantuml.com/plantuml/svg/iqhEpoikKKZEI2nIq2fGi5LGIxJcKe22jFoYR3W71gp0EX2AiZDpSb1qA2Ic9sRcOT47j8CCRPZEIirBGN4Po2W0
  • Step 2 : Remove all keywords after const

const *p = &a;
https://www.plantuml.com/plantuml/svg/iqhEpoikKT0gKB1LK4kqvbA0WhJyecmu1mQim8eHYh8pSt9GT2WafYTcvc7gmpG_DIy5nGgGKG0=
  • Step 3 : Remove everything before const

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

const *p;
https://www.plantuml.com/plantuml/svg/iqhEpoikKT0giEPIW8Aq_A9iE0S6hC3g48goCtDoK7Ge9AOdPkPXwaCnEZKl1SLqa2a0
  • 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

char a = 5;

const char *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

char a = 5, b = 6;

const char *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

char *const p = &a;
https://www.plantuml.com/plantuml/svg/iqhEI2nIq4hEpoikKIXGi5LGIxIsvbA0WhJyecmu1mQi85eGgh8pSt9Gj2WafYTcvc7H2BA537qmXy5bKlDIK9m6CWa0
  • Step 2 : Remove all keywords after const

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

const p = &a;
https://www.plantuml.com/plantuml/svg/iqhEpoikKIXGi5LGIxJcKe22jFoYR3W71gp0QX6AiZDpSb1qA2Ic9sRcOUh3C3urBmN51f1900==
  • Step 4 : Remove assignment

const p;
https://www.plantuml.com/plantuml/svg/iqhEpoikKIYmvbA0WhJyecmu1mQimCeGYh8pSt9GT2WafYTcvc7gGpOvDIy5nN2G8G0=
  • 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

char a = 5, b = 6;

char *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

char a = 5, b = 6;

char *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 char *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 char *const p = &a;
  • Step 2 : Remove all keywords after second const

const char *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

char a = 5, b = 6;

const char *const p = &a;

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

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

Statement

Meaning

const char a = 5;

  • a CAN NOT be changed again

char const a = 5;

  • a CAN NOT be changed again

const char *p = &a;

  • *p CAN NOT be changed

  • p can be changed

char const *p = &a;

  • *p CAN NOT be changed

  • p can be changed

char *const p = &a;

  • *p can be changed

  • p CAN NOT be changed

const char *const p = &a;

  • *p CAN NOT be changed

  • p CAN NOT be changed