const struct single pointer

In this section, you are going to learn

What is const struct ABC ?

What is struct ABC const ?

What is const struct ABC *p = &a; ?

What is struct ABC const *p = &a; ?

What is struct ABC *const p = &a; ?

What is const struct ABC *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

struct ABC
{
        int x;
        int y;
};

const struct ABC a = { .x = 10, .y = 20 };
  • Step 2 : Remove all keywords after const

const a = { .x = 10, .y = 20 };
  • Step 3 : Remove everything before const

const a = { .x = 10, .y = 20 };
  • Step 4 : Remove assignment

const a;
  • 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

struct ABC
{
        int x;
        int y;
};

const struct ABC a = { .x = 10, .y = 20 };

a.x = 100; // --> This is invalid

a.y = 200; // --> This is invalid
struct ABC
{
        int x;
        int y;
};

const struct ABC a = { .x = 10, .y = 20 };
struct ABC b = { .x = 100, .y = 200 };

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

const struct ABC *p = &a;
  • Step 2 : Remove all keywords after const

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

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

const *p;
  • 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

struct ABC
{
        int x;
        int y;
};

struct ABC a = { .x = 10, .y = 20 };

const struct ABC *p = &a;

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

p->y = 200; // --> 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

struct ABC a , b;

const struct ABC *p = &a;

p = &b; // --> This is valid
  • Step 1 : Consider the statement

struct ABC *const p = &a;
  • Step 2 : Remove all keywords after const

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

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

const p;
  • 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

struct ABC a, b;

struct ABC *const p = &a;

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

struct ABC a, b;

struct ABC *const p = &a;

p->x = 100; // --> This is valid
p->y = 200; // --> This is valid
  • 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 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 struct ABC *const p = &a;
  • Step 2 : Remove all keywords after second const

const struct ABC *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

struct ABC a, b;

const struct ABC *const p = &a;

p->x = 100; // --> This is invalid
p->y = 200; // --> This is invalid

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

Statement

Meaning

const struct ABC a;

  • a CAN NOT be changed again

struct ABC const a;

  • a CAN NOT be changed again

const struct ABC *p;

  • *p CAN NOT be changed

  • p can be changed

struct ABC const *p;

  • *p CAN NOT be changed

  • p can be changed

struct ABC *const p;

  • *p can be changed

  • p CAN NOT be changed

const struct ABC *const p;

  • *p CAN NOT be changed

  • p CAN NOT be changed