const int double pointer

In this section, you are going to learn

What is const int **q; ?

What is const int *const *q; ?

What is const int *const *const q; ?

  • 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 **q;
  • Step 2 : Remove all keywords after const

const **q;
  • Step 3 : Remove everything before const

const **q;
  • Step 4 : Remove assignment

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

  • We see **q after const

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

int a = 5;
int *p;
const int **q;

p = &a;
q = &p;
**q = 100; // --> This is invalid
  • Step 6 : Bonus point ! q, *q, **q are different

Means, q and *q can still be changed

int a = 5, b = 6;
int *x, *y;
const int **q;

x = &a;
q = &x;  // --> This is valid

y = &b;
*q = y; // --> This is valid

printf("**q = %d\n", **q); // --> prints 6
  • There are two occurences of const keyword

  • Hence, let us apply the same rules two times

  • Step 1 : Consider the statement

const int *const *q;
  • Step 2 : Remove all keywords after first const

const **q;
  • Step 3 : Remove everything before first const

const **q;
  • Step 4 : Remove assignment

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

**q CAN NOT be changed

  • Step 1 : Consider the statement

const int *const *q;
  • Step 2 : Remove all keywords after second const

const int *const *q;
  • Step 3 : Remove everything before second const

const *q;
  • Step 4 : Remove assignment

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

*q CAN NOT be changed

  • **q, *q CAN NOT be changed

  • q can be changed

  • There are three occurences of const keyword

  • Hence, let us apply the same rules three times

  • Step 1 : Consider the statement

const int *const *const q;
  • Step 2 : Remove all keywords after first const

const **q;
  • Step 3 : Remove everything before first const

const **q;
  • Step 4 : Remove assignment

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

  • **q CAN NOT be changed

  • Step 1 : Consider the statement

const int *const *const q;
  • Step 2 : Remove all keywords after second const

const int *const *q;
  • Step 3 : Remove everything before second const

const *q;
  • Step 4 : Remove assignment

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

*q CAN NOT be changed

  • Step 1 : Consider the statement

const int *const *const q;
  • Step 2 : Remove all keywords after third const

const int *const *const q;
  • Step 3 : Remove everything before third const

const q;
  • Step 4 : Remove assignment

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

  • q CAN NOT be changed

**q, *q, q CAN NOT be changed

Statement

Meaning

const int **q;

  • **q CAN NOT be changed again

  • *q can be changed again

  • q can be changed again

const int *const *q;

  • **q CAN NOT be changed again

  • *q CAN NOT be changed again

  • q can be changed again

const int *const *const q;

  • **q CAN NOT be changed again

  • *q CAN NOT be changed again

  • q CAN NOT be changed again