const structure triple pointer

In this section, you are going to learn

What is const struct ABC ***r; ?

What is const struct ABC *const **r; ?

What is const struct ABC *const *const *r; ?

What is const struct ABC *const *const *const r; ?

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

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

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

const ***r;
  • Step 4 : Remove assignment

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

  • We see ***r after const

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

  • Step 6 : Bonus point ! r, *r, **r, ***r are different

Means, r, *r, **r can still be changed

  • There are two occurences of const keyword

  • Hence, let us apply the same rules two times

  • Step 1 : Consider the statement

const struct ABC *const **r
  • Step 2 : Remove all keywords after first const

const ***r
  • Step 3 : Remove everything before first const

const ***r
  • Step 4 : Remove assignment

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

***r CAN NOT be changed

  • Step 1 : Consider the statement

const struct ABC *const **r
  • Step 2 : Remove all keywords after second const

const struct ABC *const **r
  • Step 3 : Remove everything before second const

const **r
  • Step 4 : Remove assignment

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

**r CAN NOT be changed

  • ***r, **r CAN NOT be changed

  • *r, r 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 struct ABC *const *const *r;
  • Step 2 : Remove all keywords after first const

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

const ***r;
  • Step 4 : Remove assignment

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

***r CAN NOT be changed

  • Step 1 : Consider the statement

const struct ABC *const *const *r;
  • Step 2 : Remove all keywords after second const

const struct ABC *const **r;
  • Step 3 : Remove everything before second const

const **r;
  • Step 4 : Remove assignment

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

**r CAN NOT be changed

  • Step 1 : Consider the statement

const struct ABC *const *const *r;
  • Step 2 : Remove all keywords after third const

const struct ABC *const *const *r;
  • Step 3 : Remove everything before third const

const *r;
  • Step 4 : Remove assignment

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

*r CAN NOT be changed

  • ***r, **r, *r, CAN NOT be changed

  • r can be changed

  • There are Four occurences of const keyword

  • Hence, let us apply the same rules Four times

  • Step 1 : Consider the statement

const struct ABC *const *const *const r;
  • Step 2 : Remove all keywords after first const

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

const ***r;
  • Step 4 : Remove assignment

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

***r CAN NOT be changed

  • Step 1 : Consider the statement

const struct ABC *const *const *const r;
  • Step 2 : Remove all keywords after second const

const struct ABC *const **r;
  • Step 3 : Remove everything before second const

const **r;
  • Step 4 : Remove assignment

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

**r CAN NOT be changed

  • Step 1 : Consider the statement

const struct ABC *const *const *const r;
  • Step 2 : Remove all keywords after third const

const struct ABC *const *const *r;
  • Step 3 : Remove everything before third const

const *r;
  • Step 4 : Remove assignment

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

*r CAN NOT be changed

  • Step 1 : Consider the statement

const struct ABC *const *const *const r;
  • Step 2 : Remove all keywords after fourth const

const struct ABC *const *const *const r;
  • Step 3 : Remove everything before fourth const

const r;
  • Step 4 : Remove assignment

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

r CAN NOT be changed

***r, **r, *r, r CAN NOT be changed

Statement

Meaning

const struct ABC ***r;

  • ***r CAN NOT be changed again

  • **r can be changed again

  • *r can be changed again

  • r can be changed again

const struct ABC *const **r;

  • ***r CAN NOT be changed again

  • **r CAN NOT be changed again

  • *r can be changed again

  • r can be changed again

const struct ABC *const *const *r;

  • ***r CAN NOT be changed again

  • **r CAN NOT be changed again

  • *r CAN NOT be changed again

  • r can be changed again

const struct ABC *const *const *const r;

  • ***r CAN NOT be changed again

  • **r CAN NOT be changed again

  • *r CAN NOT be changed again

  • r can be changed again