Equations of Structure Triple Pointer

In this section, you are going to learn

What are the different ways of declarations ?

How to dervie equations ?

What are the Properties of Variable ?

What are the Properties of Expression ?

1. Equations of Triple Pointer

In this section, you are going to learn

  • How to derive pointer equations for Triple pointer ?

  • How to apply these equations to understand C statements ?

You can derive equations looking at C declarations !

There are many methods in C, using which a Triple pointer can be declared. See below

 1#include <stdio.h>
 2
 3struct ABC
 4{
 5        int a;
 6        int b;
 7        int c;
 8};
 9
10int main(void)
11{
12        struct ABC x = {.a = 1, .b = 2, .c = 3}, *p = &x, **q = &p, ***r = &q;
13
14        printf("x.a = %d\n", x.a);
15        printf("x.b = %d\n", x.b);
16        printf("x.c = %d\n", x.c);
17
18        printf("(***r).a = %d\n", (***r).a);
19        printf("(***r).b = %d\n", (***r).b);
20        printf("(***r).c = %d\n", (***r).c);
21
22        printf("(**r)->a = %d\n", (**r)->a);
23        printf("(**r)->b = %d\n", (**r)->b);
24        printf("(**r)->c = %d\n", (**r)->c);
25
26        printf("(*r)[0][0].a = %d\n", (*r)[0][0].a);
27        printf("(*r)[0][0].b = %d\n", (*r)[0][0].b);
28        printf("(*r)[0][0].c = %d\n", (*r)[0][0].c);
29
30        printf("(**r)[0].a = %d\n", (**r)[0].a);
31        printf("(**r)[0].b = %d\n", (**r)[0].b);
32        printf("(**r)[0].c = %d\n", (**r)[0].c);
33
34        printf("r[0][0][0].a = %d\n", r[0][0][0].a);
35        printf("r[0][0][0].b = %d\n", r[0][0][0].b);
36        printf("r[0][0][0].c = %d\n", r[0][0][0].c);
37
38        return 0;
39}
  • Output is as below

x.a = 1
x.b = 2
x.c = 3

(***r).a = 1
(***r).b = 2
(***r).c = 3

(**r)->a = 1
(**r)->b = 2
(**r)->c = 3

(*r)[0][0].a = 1
(*r)[0][0].b = 2
(*r)[0][0].c = 3

(**r)[0].a = 1
(**r)[0].b = 2
(**r)[0].c = 3

r[0][0][0].a = 1
r[0][0][0].b = 2
r[0][0][0].c = 3
In this example,
  • x is a structure

  • p is an single structure pointer

  • q is a double structure pointer

  • r is a triple structure pointer

  • x, p, q, r are declared in one Single Line

  • x, p, q, r are assigned in one Single Line

  • x is assigned with value 10

  • p is assigned with address of x

  • q is assigned with address of p

  • r is assigned with address of q

 1#include <stdio.h>
 2
 3struct ABC
 4{
 5        int a;
 6        int b;
 7        int c;
 8};
 9
10int main(void)
11{
12        struct ABC x = {.a = 1, .b = 2, .c = 3};
13
14        struct ABC *p = &x;
15
16        struct ABC **q = &p;
17
18        struct ABC ***r = &q;
19
20        printf("x.a = %d\n", x.a);
21        printf("x.b = %d\n", x.b);
22        printf("x.c = %d\n", x.c);
23
24        printf("(***r).a = %d\n", (***r).a);
25        printf("(***r).b = %d\n", (***r).b);
26        printf("(***r).c = %d\n", (***r).c);
27
28        printf("(**r)->a = %d\n", (**r)->a);
29        printf("(**r)->b = %d\n", (**r)->b);
30        printf("(**r)->c = %d\n", (**r)->c);
31
32        printf("(*r)[0][0].a = %d\n", (*r)[0][0].a);
33        printf("(*r)[0][0].b = %d\n", (*r)[0][0].b);
34        printf("(*r)[0][0].c = %d\n", (*r)[0][0].c);
35
36        printf("(**r)[0].a = %d\n", (**r)[0].a);
37        printf("(**r)[0].b = %d\n", (**r)[0].b);
38        printf("(**r)[0].c = %d\n", (**r)[0].c);
39
40        printf("r[0][0][0].a = %d\n", r[0][0][0].a);
41        printf("r[0][0][0].b = %d\n", r[0][0][0].b);
42        printf("r[0][0][0].c = %d\n", r[0][0][0].c);
43
44        return 0;
45}
  • Output is as below

x.a = 1
x.b = 2
x.c = 3

(***r).a = 1
(***r).b = 2
(***r).c = 3

(**r)->a = 1
(**r)->b = 2
(**r)->c = 3

(*r)[0][0].a = 1
(*r)[0][0].b = 2
(*r)[0][0].c = 3

(**r)[0].a = 1
(**r)[0].b = 2
(**r)[0].c = 3

r[0][0][0].a = 1
r[0][0][0].b = 2
r[0][0][0].c = 3
In this example,
  • x is a structure

  • p is a single structure pointer

  • q is a double structure pointer

  • r is a triple structure pointer

  • x is declared in a separate line

  • p is declared in a separate line

  • q is declared in a separate line

  • r is declared in a separate line

  • x is assigned in the same line of declaration

  • p is assigned in the same line of declaration

  • q is assigned in the same line of declaration

  • r is assigned in the same line of declaration

  • x is assigned with value 10

  • p is assigned with address of x

  • q is assigned with address of p

  • r is assigned with address of q

 1#include <stdio.h>
 2
 3struct ABC
 4{
 5        int a;
 6        int b;
 7        int c;
 8};
 9
10int main(void)
11{
12        struct ABC x;
13
14        struct ABC *p;
15
16        struct ABC **q;
17
18        struct ABC ***r;
19
20        x.a = 1;
21        x.b = 2;
22        x.c = 3;
23
24        p = &x;
25
26        q = &p;
27
28        printf("x.a = %d\n", x.a);
29        printf("x.b = %d\n", x.b);
30        printf("x.c = %d\n", x.c);
31
32        printf("(***r).a = %d\n", (***r).a);
33        printf("(***r).b = %d\n", (***r).b);
34        printf("(***r).c = %d\n", (***r).c);
35
36        printf("(**r)->a = %d\n", (**r)->a);
37        printf("(**r)->b = %d\n", (**r)->b);
38        printf("(**r)->c = %d\n", (**r)->c);
39
40        printf("(*r)[0][0].a = %d\n", (*r)[0][0].a);
41        printf("(*r)[0][0].b = %d\n", (*r)[0][0].b);
42        printf("(*r)[0][0].c = %d\n", (*r)[0][0].c);
43
44        printf("(**r)[0].a = %d\n", (**r)[0].a);
45        printf("(**r)[0].b = %d\n", (**r)[0].b);
46        printf("(**r)[0].c = %d\n", (**r)[0].c);
47
48        printf("r[0][0][0].a = %d\n", r[0][0][0].a);
49        printf("r[0][0][0].b = %d\n", r[0][0][0].b);
50        printf("r[0][0][0].c = %d\n", r[0][0][0].c);
51
52        return 0;
53}
  • Output is as below

x.a = 1
x.b = 2
x.c = 3

(***r).a = 1
(***r).b = 2
(***r).c = 3

(**r)->a = 1
(**r)->b = 2
(**r)->c = 3

(*r)[0][0].a = 1
(*r)[0][0].b = 2
(*r)[0][0].c = 3

(**r)[0].a = 1
(**r)[0].b = 2
(**r)[0].c = 3

r[0][0][0].a = 1
r[0][0][0].b = 2
r[0][0][0].c = 3
In this example,
  • x is a structure

  • p is a single structure pointer

  • q is a double structure pointer

  • r is a triple structure pointer

  • x is declared in a separate line

  • p is declared in a separate line

  • q is declared in a separate line

  • r is declared in a separate line

  • x is assigned and is not part of declaration

  • p is assigned and is not part of declaration

  • q is assigned and is not part of declaration

  • r is assigned and is not part of declaration

  • x is assigned with value 10

  • p is assigned with address of x

  • q is assigned with address of p

  • r is assigned with address of q

Decl #

Declaration

Description

Decl 1

  • struct ABC x, *p = &x, **q = &p, ***r = &q;

Structure x, Single Pointer p, Double Pointer q, Triple Pointer r are declared and assigned in same line

Decl 2

  • struct ABC x;

  • struct ABC *p = &x;

  • struct ABC **q = &p;

  • struct ABC ***r = &q;

Structure x, Single Pointer p, Double Pointer q, Triple Pointer r are declared and assigned in separate lines

Decl 3

  • struct ABC x;

  • struct ABC *p;

  • struct ABC **q;

  • struct ABC ***r;
    • x.a = 1;

    • x.b = 2;

    • x.c = 3;

  • p = &x;

  • q = &p;

  • r = &q;

Structure x, Single Pointer p, Double Pointer q, Triple Pointer r are declared in one line and assigned in another line

  • Whenever we see any of the above methods of declarations, we need to rewrite them such that, declarations and assignments are not in same line. Similar to Declaration 3

  • Equation 1 : Obtained from Step 2

p = &x;
  • Equation 2 : Move & to the left of First Equation. It turns in to *

*p = x;
  • Equation 3 : * and [0] can be used interchangeably. Hence *p and p[0] are one and the same !

p[0] = x;
  • Equation 4 : From Equation 2, Equation 3, we can derive that x, *p, p[0] all are same !

*p = p[0] = x;
  • Equation 5 : Obtained from Step 2

q = &p;
  • Equation 6 : Move & to the left of First Equation. It turns in to *

*q = p;
  • Equation 7 : * and [0] can be used interchangeably. Hence *q and q[0] are one and the same !

q[0] = p;
  • Equation 8 : From Equation 6, Equation 7, we can derive that p, *q, q[0] all are same !

*q = q[0] = p;
  • Equation 9 : From Equation 1, we know p = &x. Hence Replace p with &x in Equation 8

*q = q[0] = &x;
  • Equation 10 : Move & to the left of Equation 9. It turns in to *

**q = *q[0] = x;
  • Equation 11 : * and [0] can be used interchangeably

q[0][0] = x;
  • Equation 12 : Obtained from Step 2

r = &q;
  • Equation 13 : Move & to the left of First Equation. It turns in to *

*r = q;
  • Equation 14 : * and [0] can be used interchangeably. Hence *r and r[0] are one and the same !

r[0] = q;
  • Equation 15 : From Equation 13, Equation 14, we can derive that q, *r, r[0] all are same !

*r = r[0] = q;
  • Equation 16 : From Equation 5, we know q = &p. Hence Replace q with &p in Equation 15

*r = r[0] = &p;
  • Equation 17 : Move & to the left of Equation 16. It turns in to *

**r = *r[0] = p;
  • Equation 18 : * and [0] can be used interchangeably

r[0][0] = p;
  • Equation 19 : Comparing Equation 17, 18 we can conclude **r, *r[0], r[0][0] and p all are same !

**r = *r[0] = r[0][0] = p;
  • Equation 20 : From Equation 1, p = &x. Hence replace p with &x in Equation 19

**r = *r[0] = r[0][0] = &x;
  • Equation 21 : Move & to the left of Equation 16. It turns in to *

***r = **r[0] = *r[0][0] = x;
  • Equation 22 : * and [0] can be used interchangeably

***r = **r[0] = r[0][0][0] = x;

Equation #

Equation

Description

Equation 1

p = &x

Base condition

Equation 2

*p = x

From Equation 1, Move & from RHS to LHS to get * on LHS

Equation 3

p[0] = x

* and [0] can be used interchangeably. Hence *p and p[0] are synonyms

Equation 4

*p = p[0] = x

From Equation 2, 3 we can conclude *p, p[0], x are synonyms

Equation #

Equation

Description

Equation 5

q = &p

Base condition

Equation 6

*q = p

From Equation 5, Move & from RHS to LHS to get * on LHS

Equation 7

q[0] = p

* and [0] can be used interchangeably. Hence *q and q[0] are synonyms

Equation 8

*q = q[0] = p

From Equation 6, Equation 7, we can derive that p, *q, q[0] all are same !

Equation 9

*q = q[0] = &x

From Equation 1, we know p = &x. Hence Replace p with &x in Equation 8

Equation 10

**q = *q[0] = x

From Equation 9, Move & from RHS to LHS to get * on LHS

Equation 11

q[0][0] = x

* and [0] can be used interchangeably

Equation #

Equation

Description

Equation 12

r = &q

Base condition

Equation 13

*r = q

From Equation 12, Move & from RHS to LHS to get * on LHS

Equation 14

r[0] = q

* and [0] can be used interchangeably. Hence *r and r[0] are synonyms

Equation 15

*r = r[0] = q

From Equation 13, Equation 14, we can derive that q, *r, r[0] all are same !

Equation 16

*r = r[0] = &p

From Equation 5, we know q = &p. Hence Replace q with &p in Equation 15

Equation 17

**r = *r[0] = p

Move & to the left of Equation 16. It turns in to *

Equation 18

q[0][0] = p

* and [0] can be used interchangeably

Equation 19

**r = *r[0] = r[0][0] = p;

Comparing Equation 17, 18 we can conclude **r, *r[0], r[0][0] and p all are same !

Equation 20

**r = *r[0] = r[0][0] = &x;

From Equation 1, p = &x. Hence replace p with &x in Equation 19

Equation 21

***r = **r[0] = *r[0][0] = x;

Move & to the left of Equation 16. It turns in to *

Equation 22

***r = **r[0] = r[0][0][0] = x;

* and [0] can be used interchangeably

(*p).a = 10;
(*p).b = 20;
(*p).c = 30;

printf("(*p).a = %d\n", (*p).a);
printf("(*p).b = %d\n", (*p).b);
printf("(*p).c = %d\n", (*p).c);

Output is as below

(*p).a = 10
(*p).b = 20
(*p).c = 30
p[0].a = 10;
p[0].b = 20;
p[0].c = 30;

printf("p[0].a = %d\n", p[0].a);
printf("p[0].b = %d\n", p[0].b);
printf("p[0].c = %d\n", p[0].c);

Output is as below

p[0].a = 10
p[0].b = 20
p[0].c = 30
x.a = 100;

printf("x.a = %d\n", x.a);
printf("(*p).a = %d\n", (*p).a);
printf("p[0].a = %d\n", p[0].a);
printf("p->a = %d\n", p->a);

Output is as below

x.a    = 100
(*p).a = 100
p[0].a = 100
p->a   = 100
p->a = 10;
p->b = 20;
p->c = 30;

printf("p->a = %d\n", p->a);
printf("p->b = %d\n", p->b);
printf("p->c = %d\n", p->c);

Output is as below

p->a = 10
p->b = 20
p->c = 30
(**q).a = 100;

printf("x.a = %d, (**q).a = %d\n", x.a, (**q).a);
Output :

x.a = 100, (**q).a = 100

(*q)[0].a = 100;

printf("x.a = %d, (*q)[0].a = %d\n", x.a, (*q)[0].a);
Output :

x.a = 100, (*q)[0].a = 100

q[0][0].a = 100;

printf("x.a = %d, q[0][0].a = %d\n", x.a, q[0][0].a);
Output :

x.a = 100, q[0][0].a = 100

struct ABC x, y, *p, **q;

x.a = 100;
p = &x;
q = &p;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d\n", (*p).a, p[0].a, (**q).a);

y.a = 200;

// *q, p are synonyms.
// Means *q = &y also means p = &y
*q = &y;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d\n", (*p).a, p[0].a, (**q).a);
Output :

(*p).a = 100, p[0].a = 100, (**q).a = 100

(*p).a = 200, p[0].a = 200, (**q).a = 200

struct ABC x, y, *p, **q;

x.a = 100;
p = &x;
q = &p;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d\n", (*p).a, p[0].a, (**q).a);

y.a = 200;

// q[0], p are synonyms.
// Means q[0] = &y also means p = &y
q[0] = &y;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d\n", (*p).a, p[0].a, (**q).a);
Output :

(*p).a = 100, p[0].a = 100, (**q).a = 100

(***r).a = 100;

printf("x.a = %d, (***r).a = %d\n", x.a, (***r).a);
Output :

x.a = 100, (***r).a = 100

(**r)[0].a = 100;

printf("x.a = %d, (**r[0]).a = %d\n", x.a, (**r[0]).a);
Output :

x.a = 100, (**r[0]).a = 100

(*r)[0][0].a = 100;

printf("x.a = %d, (*r)[0][0].a = %d\n", x.a, (*r)[0][0].a);
Output :

x.a = 100, (*r)[0][0].a = 100

r[0][0][0].a = 100;

printf("x.a = %d, r[0][0][0].a = %d\n", x.a, r[0][0][0].a);
Output :

x.a = 100, r[0][0][0].a = 100

(**r)->a = 100;

printf("x.a = %d, (**r)->a = %d\n", x.a, (**r)->a);
Output :

x.a = 100, (**r)->a = 100

(*r[0])->a = 100;

printf("x.a = %d, (*r[0])->a = %d\n", x.a, (*r[0])->a);
Output :

x.a = 100, (*r[0])->a = 100

(r[0][0])->a = 100;

printf("x.a = %d, (r[0][0])->a = %d\n", x.a, (r[0][0])->a);
Output :

x.a = 100, (r[0][0])->a = 100

struct ABC x, y, *p, **q, ***r;

x.a = 100;
p = &x;
q = &p;
r = &q;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);

y.a = 200;

**r = &y;
//Step 1 : Apply Equation 19 : **r = &y also means  p = &y
//Step 2 : Now all equations having p like Equation 6, 7, 8 are affected  because *q = p
//         Now, *q = &y, q[0] = &y since p, *q, q[0] all are same
//         Now, **q = y, *q[0] = y, q[0][0] = y
//Step 3 : ***r, *r[0], r[0][0][0] = y also means  *p = y

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);
Output :

(*p).a = 100, p[0].a = 100, (**q).a = 100, (***r).a = 100

(*p).a = 200, p[0].a = 200, (**q).a = 200, (***r).a = 200

struct ABC x, y, *p, **q, ***r;

x.a = 100;
p = &x;
q = &p;
r = &q;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);

y.a = 200;

*r[0] = &y;
//Step 1 : Apply Equation 19 : *r[0] = &y also means  p = &y
//Step 2 : Now all equations having p like Equation 6, 7, 8 are affected  because *q = p
//         Now, *q = &y, q[0] = &y since p, *q, q[0] all are same
//         Now, **q = y, *q[0] = y, q[0][0] = y
//Step 3 : ***r, *r[0], r[0][0][0] = y also means  *p = y

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);
Output :

(*p).a = 100, p[0].a = 100, (**q).a = 100, (***r).a = 100

(*p).a = 200, p[0].a = 200, (**q).a = 200, (***r).a = 200

struct ABC x, y, *p, **q, ***r;

x.a = 100;
p = &x;
q = &p;
r = &q;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);

y.a = 200;

r[0][0] = &y;
//Step 1 : Apply Equation 19 : r[0][0] = &y also means  p = &y
//Step 2 : Now all equations having p like Equation 6, 7, 8 are affected because *q = p
//         Now, *q = &y, q[0] = &y since p, *q, q[0] all are same
//         Now, **q = y, *q[0] = y, q[0][0] = y
//Step 3 : ***r, *r[0], r[0][0][0] = y also means  *p = y

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);
Output :

(*p).a = 100, p[0].a = 100, (**q).a = 100, (***r).a = 100

(*p).a = 200, p[0].a = 200, (**q).a = 200, (***r).a = 200

struct ABC x, *p, **q, ***r;

struct ABC m, *p1;

x.a = 100;
p = &x;
q = &p;
r = &q;

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);

m.a = 200;
p1 = &m;

*r = &p1;
//Step 1 : Apply Equation 15 : *r = &p1 also means q = &p1
//Step 2 : **r = p1 and *q = p1
//Step 3 : **r = &m and *q = &m
//Step 4 : ***r = m and **q = m

//Note p is no changed so far

printf("(*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", (*p).a, p[0].a, (**q).a, (***r).a);
Output :

(*p).a = 100, p[0].a = 100, (**q).a = 100, (***r).a = 100

(*p).a = 100, p[0].a = 100, (**q).a = 200, (***r).a = 200

 1#include <stdio.h>
 2
 3struct ABC
 4{
 5        int a;
 6        int b;
 7        int c;
 8};
 9
10int main(void)
11{
12        struct ABC x;
13
14        struct ABC *p;
15
16        struct ABC **q;
17
18        struct ABC ***r;
19
20        x.a = 10;
21
22        p = &x;
23
24        q = &p;
25
26        r = &q;
27
28        printf("x.a = %d, (*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", x.a, (*p).a, p[0].a, (**q).a, (***r).a);
29
30        x.a = 20;
31
32        printf("x.a = %d, (*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", x.a, (*p).a, p[0].a, (**q).a, (***r).a);
33
34        (*p).a = 30;
35
36        printf("x.a = %d, (*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", x.a, (*p).a, p[0].a, (**q).a, (***r).a);
37
38        p[0].a = 40;
39
40        printf("x.a = %d, (*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", x.a, (*p).a, p[0].a, (**q).a, (***r).a);
41
42        (**q).a = 50;
43
44        printf("x.a = %d, (*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", x.a, (*p).a, p[0].a, (**q).a, (***r).a);
45
46        (***r).a = 60;
47
48        printf("x.a = %d, (*p).a = %d, p[0].a = %d, (**q).a = %d, (***r).a = %d\n", x.a, (*p).a, p[0].a, (**q).a, (***r).a);
49
50        return 0;
51}
  • Output is as below

x.a = 10, (*p).a = 10, p[0].a = 10, (**q).a = 10, (***r).a = 10

x.a = 20, (*p).a = 20, p[0].a = 20, (**q).a = 20, (***r).a = 20

x.a = 30, (*p).a = 30, p[0].a = 30, (**q).a = 30, (***r).a = 30

x.a = 40, (*p).a = 40, p[0].a = 40, (**q).a = 40, (***r).a = 40

x.a = 50, (*p).a = 50, p[0].a = 50, (**q).a = 50, (***r).a = 50

x.a = 60, (*p).a = 60, p[0].a = 60, (**q).a = 60, (***r).a = 60

2. Properties of a variable

In this section, you are going to learn

  • Properties of a variable

    • Type of a variable ?

    • Size of a variable ?

    • Scope, Lifetime and Memory of a variable ?

 1struct ABC x;
 2
 3struct ABC *p;
 4
 5struct ABC **q;
 6
 7struct ABC ***r;
 8
 9p = &x;
10
11q = &p;
12
13r = &q;
  • In above code snippet, there are four variables x, p, q, r

Variable

Type

Description

type_of(x)

struct ABC

See Line 1

type_of(p)

struct ABC *

See Line 3

type_of(q)

struct ABC **

See Line 5

type_of(r)

struct ABC ***

See Line 7

Adress of variable must be stored in next level pointer type always

 1struct ABC x;
 2
 3struct ABC *p;
 4
 5struct ABC **q;
 6
 7struct ABC ***r;
 8
 9p = &x;
10
11q = &p;
12
13r = &q;
  • In above code snippet, there are four variables x, p, q, r

Variable

Type

Description

type_of(&x)

struct ABC *

  • type_of(x) is struct ABC

  • Hence, type_of(&x) is struct ABC *

type_of(&p)

struct ABC **

  • type_of(p) is struct ABC *

  • Hence, type_of(&p) is struct ABC **

type_of(&q)

struct ABC ***

  • type_of(q) is struct ABC **

  • Hence, type_of(&q) is struct ABC ***

type_of(&r)

struct ABC ****

  • type_of(r) is struct ABC ***

  • Hence, type_of(&r) is struct ABC ****

Sizeof(type)

Size

sizeof(char)

1 Byte

sizeof(int)

4 Bytes

sizeof(float)

4 Bytes

sizeof(double)

8 Bytes

sizeof(pointer types) is always 8 Bytes, where pointer is single, double, triple etc.,:

Sizeof(type *)

Size

sizeof(char *)

8 Bytes

sizeof(int *)

8 Bytes

sizeof(float *)

8 Bytes

sizeof(double *)

8 Bytes

sizeof(struct xyz *)

8 Bytes

sizeof(union xyz *)

8 Bytes

Sizeof(type **)

Size

sizeof(char **)

8 Bytes

sizeof(int **)

8 Bytes

sizeof(float **)

8 Bytes

sizeof(double **)

8 Bytes

sizeof(struct xyz **)

8 Bytes

sizeof(union xyz **)

8 Bytes

etc.,

Sizeof(type ***)

Size

sizeof(char ***)

8 Bytes

sizeof(int ***)

8 Bytes

sizeof(float ***)

8 Bytes

sizeof(double ***)

8 Bytes

sizeof(struct xyz ***)

8 Bytes

sizeof(union xyz ***)

8 Bytes

etc.,

sizeof(&variable) is always 8 Bytes, where type_of(variable) can be anything

Sizeof(&variable)

Size

Declaration

sizeof(&x)

8 Bytes

char x;

sizeof(&x)

8 Bytes

int x;

sizeof(&x)

8 Bytes

float x;

sizeof(&x)

8 Bytes

double x;

sizeof(&x)

8 Bytes

struct xyz x;

sizeof(&x)

8 Bytes

union xyz x;

Sizeof(&variable)

Size

Declaration

sizeof(&x)

8 Bytes

char *x;

sizeof(&x)

8 Bytes

int *x;

sizeof(&x)

8 Bytes

float *x;

sizeof(&x)

8 Bytes

double *x;

sizeof(&x)

8 Bytes

struct xyz *x;

sizeof(&x)

8 Bytes

union xyz *x;

Sizeof(&variable)

Size

Declaration

sizeof(&x)

8 Bytes

char **x;

sizeof(&x)

8 Bytes

int **x;

sizeof(&x)

8 Bytes

float **x;

sizeof(&x)

8 Bytes

double **x;

sizeof(&x)

8 Bytes

struct xyz **x;

sizeof(&x)

8 Bytes

union xyz **x;

Sizeof(&variable)

Size

Declaration

sizeof(&x)

8 Bytes

char ***x;

sizeof(&x)

8 Bytes

int ***x;

sizeof(&x)

8 Bytes

float ***x;

sizeof(&x)

8 Bytes

double ***x;

sizeof(&x)

8 Bytes

struct xyz ***x;

sizeof(&x)

8 Bytes

union xyz ***x;

sizeof(variable) equals sizeof(typeof(variable))

 1struct ABC x;
 2
 3struct ABC *p;
 4
 5struct ABC **q;
 6
 7struct ABC ***r;
 8
 9p = &x;
10
11q = &p;
  • In above code snippet, there are four variables x, p, q, r

Sizeof(Variable)

Size

Description

sizeof(x)

12 Bytes

  • How ?
    • Step 1 : sizeof(x) equals sizeof(typeof(x))

    • Step 2 : type_of(x) is struct ABC

    • Step 3 : sizeof(struct ABC) is 12 Bytes

    • Hence, sizeof(x) is 12 Bytes

sizeof(p)

8 Bytes

  • How ?
    • Step 1 : sizeof(p) equals sizeof(typeof(p))

    • Step 2 : type_of(p) is struct ABC *

    • Step 3 : sizeof(struct ABC *) is 8 Bytes

    • Hence, sizeof(p) is 8 Bytes

sizeof(q)

8 Bytes

  • How ?
    • Step 1 : sizeof(q) equals sizeof(typeof(q))

    • Step 2 : type_of(q) is struct ABC **

    • Step 3 : sizeof(struct ABC **) is 8 Bytes

    • Hence, sizeof(q) is 8 Bytes

sizeof(r)

8 Bytes

  • How ?
    • Step 1 : sizeof(r) equals sizeof(typeof(r))

    • Step 2 : type_of(r) is struct ABC ***

    • Step 3 : sizeof(struct ABC ***) is 8 Bytes

    • Hence, sizeof(r) is 8 Bytes

  • Global Scope and Lifetime.

  • Local Scope and Lifetime

3. Properties of Expressions

In this section, you are going to learn

  • Properties of Expressions

    • What is an Expression ?

    • Table of Expressions

    • Table of Size (for Expressions)

    • Table of Type (for Expressions)

    • Table of Address/Value (for Expression)

    • Table of Function Prototype (for Expression)

 1struct ABC x;
 2
 3struct ABC *p;
 4
 5struct ABC **q;
 6
 7struct ABC ***r;
 8
 9p = &x;
10
11q = &p;
12
13r = &q;

Expression

Description

x

  • x is a structure

&x

  • &x is address of a structure

  • &x is a single pointer

p

  • p is a pointer to a structure

  • p is a single pointer

&p

  • &p is address of a pointer

  • &p is a double pointer

*p

p[0]

q

  • q is a double pointer and holds the address of a single pointer

&q

  • &q is address of a double pointer

  • &q is a triple pointer

*q

q[0]

**q

*q[0]

q[0][0]

r

  • r is a triple pointer and holds the address of a double pointer

&r

  • &r is a fourth pointer

*r

r[0]

**r

*r[0]

r[0][0]

***r

**r[0]

r[0][0][0]

Expression

Size

Description

sizeof(x)

12 Bytes

sizeof(&x)

8 Bytes

sizeof(p)

8 Bytes

sizeof(&p)

8 Bytes

sizeof(*p)

12 Bytes

  • Step 1 : sizeof(*p) equals sizeof(x) See Equation 2

  • Step 2 : sizeof(x) equals sizeof(type_of(x)) See Property 2.4

  • Step 3 : sizeof(type_of(x)) equals sizeof(struct ABC) See Property 1.1

  • Step 4 : sizeof(struct ABC) equals 12 Bytes

sizeof(p[0])

12 Bytes

  • Step 1 : sizeof(p[0]) equals sizeof(x)``= x See Equation 3

  • Step 2 : sizeof(x) equals sizeof(type_of(x)) See Property 2.4

  • Step 3 : sizeof(type_of(x)) equals sizeof(struct ABC) See Property 1.1

  • Step 4 : sizeof(struct ABC) equals 12 Bytes

sizeof(q)

8 Bytes

sizeof(&q)

8 Bytes

sizeof(*q)

8 Bytes

  • Step 1 : sizeof(*q) equals sizeof(p). See Equation 8

  • Step 2 : sizeof(p), equals sizeof(type_of(p)) See Property 2.4

  • Step 3 : sizeof(type_of(p)) equals sizeof(struct ABC *) See Property 1.1

  • Step 4 : sizeof(struct ABC *) equals 8 Bytes

sizeof(q[0])

8 Bytes

  • Step 1 : sizeof(q[0]) equals sizeof(p) See Equation 8

  • Step 2 : sizeof(p) equals sizeof(type_of(p)) See Property 2.4

  • Step 3 : sizeof(type_of(p)) equals sizeof(struct ABC *) See Property 1.1

  • Step 4 : sizeof(struct ABC *) equals 8 Bytes

sizeof(**q)

12 Bytes

sizeof(*q[0])

12 Bytes

sizeof(q[0][0])

12 Bytes

  • Step 1 : sizeof(q[0][0]) equals sizeof(x) See Equation 11

  • Step 2 : sizeof(x) equals sizeof(type_of(x)) See Property 2.4

  • Step 3 : sizeof(type_of(x)) equals sizeof(struct ABC) See Property 1.1

  • Step 4 : sizeof(struct ABC) equals 12 Bytes

sizeof(r)

8 Bytes

sizeof(&r)

8 Bytes

sizeof(*r)

8 Bytes

  • Step 1 : sizeof(*r) equals sizeof(q). See Equation 15

  • Step 2 : sizeof(q) equals sizeof(type_of(q))

  • Step 3 : sizeof(type_of(q)) equals sizeof(struct ABC **)

  • Step 4 : sizeof(struct ABC **) equals 8 Bytes

sizeof(r[0])

8 Bytes

  • Step 1 : sizeof(r[0]) equals sizeof(q). See Equation 15

  • Step 2 : sizeof(q) equals sizeof(type_of(q))

  • Step 3 : sizeof(type_of(q)) equals sizeof(struct ABC **)

  • Step 4 : sizeof(struct ABC **) equals 8 Bytes

sizeof(**r)

8 Bytes

  • Step 1 : sizeof(**r) equals sizeof(p). See Equation 19

  • Step 2 : sizeof(p) equals sizeof(type_of(p)) See Property 2.4

  • Step 3 : sizeof(type_of(p)) equals sizeof(struct ABC *) See Property 1.1

  • Step 4 : sizeof(struct ABC *) equals 8 Bytes

sizeof(*r[0])

8 Bytes

  • Step 1 : sizeof(*r[0]) equals sizeof(p). See Equation 19

  • Step 2 : sizeof(p) equals sizeof(type_of(p)) See Property 2.4

  • Step 3 : sizeof(type_of(p)) equals sizeof(struct ABC *) See Property 1.1

  • Step 4 : sizeof(struct ABC *) equals 8 Bytes

sizeof(r[0][0])

8 Bytes

  • Step 1 : sizeof(r[0][0]) equals sizeof(p). See Equation 19

  • Step 2 : sizeof(p) equals sizeof(type_of(p)) See Property 2.4

  • Step 3 : sizeof(type_of(p)) equals sizeof(struct ABC *) See Property 1.1

  • Step 4 : sizeof(struct ABC *) equals 8 Bytes

sizeof(***r)

12 Bytes

sizeof(**r[0])

12 Bytes

sizeof(r[0][0][0])

12 Bytes

  • Step 1 : sizeof(r[0][0][0]) equals sizeof(x). See Equation 22

  • Step 2 : sizeof(x) equals sizeof(type_of(x)) See Property 2.4

  • Step 3 : sizeof(type_of(x)) equals sizeof(struct ABC) See Property 1.1

  • Step 4 : sizeof(struct ABC) equals 12 Bytes

Expression

Type

Description

type_of(x)

struct ABC

type_of(&x)

struct ABC *

type_of(p)

struct ABC *

type_of(&p)

struct ABC **

type_of(*p)

struct ABC

  • Step 1 : type_of(*p) equals type_of(x), because *p = x. See Equation 2

  • Step 2 : type_of(x) equals struct ABC

type_of(p[0])

struct ABC

  • Step 1 : type_of(p[0]) equals type_of(x), because p[0] = x. See Equation 3

  • Step 2 : type_of(x) equals struct ABC

type_of(q)

struct ABC **

type_of(&q)

struct ABC ***

type_of(*q)

struct ABC *

type_of(q[0])

struct ABC *

type_of(**q)

struct ABC

type_of(*q[0])

struct ABC

type_of(q[0][0])

struct ABC

type_of(r)

struct ABC ***

type_of(&r)

struct ABC ****

type_of(*r)

struct ABC **

  • Step 1 : type_of(*r) equals type_of(q). See Equation 15

  • Step 2 : type_of(q) equals struct ABC **

type_of(r[0])

struct ABC **

  • Step 1 : type_of(r[0]) equals type_of(q). See Equation 15

  • Step 2 : type_of(q) equals struct ABC **

type_of(**r)

struct ABC *

type_of(*r[0])

struct ABC *

type_of(r[0][0])

struct ABC *

type_of(***r)

struct ABC

type_of(**r[0])

struct ABC

type_of(r[0][0][0])

struct ABC

Expression

Address/Value

Description

x

Value

  • Step 1 : x is a structure

  • Step 2 : Hence x is a value

&x

Address

  • & operator indicates address

p

Address

  • Step 1 : p = &x See Equation 1

  • Step 2 : & operator indicates address

&p

Address

  • & operator indicates address

*p

Value

  • Step 1 : *p is a structure. *p = x See Equation 2

  • Step 2 : Hence *p is a value

p[0]

Value

  • Step 1 : p[0] is a structure. p[0] = x See Equation 3

  • Step 2 : Hence p[0] is a value

q

Address

  • Step 1 : q = &p See Equation 5

  • Step 2 : & operator indicates address

&q

Address

  • & operator indicates address

*q

Address

  • Step 1 : *q = p See Equation 6

  • Step 2 : p = &x

  • Step 3 : & operator indicates address

q[0]

Address

  • Step 1 : q[0] = p See Equation 7

  • Step 2 : p = &x

  • Step 3 : & operator indicates address

**q

Value

  • Step 1 : **q = x See Equation 10

  • Step 2 : x is a structure

  • Step 3 : **q is a Value

*q[0]

Value

  • Step 1 : *q[0] = x See Equation 10

  • Step 2 : x is a structure

  • Step 3 : *q[0] is a Value

q[0][0]

Value

  • Step 1 : q[0][0] = x See Equation 11

  • Step 2 : x is a structure

  • Step 3 : q[0][0] is a Value

r

Address

&r

Address

  • Step 1 : & operator indicates address

*r

Address

r[0]

Address

**r

Address

*r[0]

Address

r[0][0]

Address

***r

Value

  • Step 1 : ***r = x See Equation 22

  • Step 2 : x is a structure

  • Step 3 : r[0][0][0] is a Value

**r[0]

Value

  • Step 1 : **r[0] = x See Equation 22

  • Step 2 : x is a structure

  • Step 3 : r[0][0][0] is a Value

r[0][0][0]

Value

  • Step 1 : r[0][0][0] = x See Equation 22

  • Step 2 : x is a structure

  • Step 3 : r[0][0][0] is a Value

If fun(v) is function call then, fun(type_of(v)) is the prototype

Function call

Function Prototype

Description

fun(x)

void fun(struct ABC x);

fun(&x)

void fun(struct ABC *p);

fun(p)

void fun(struct ABC *p);

fun(&p)

void fun(struct ABC **p);

fun(*p)

void fun(struct ABC x);

  • Step 1 : fun(*p) equals fun(x)See Equation 2

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

fun(p[0])

void fun(struct ABC x);

  • Step 1 : fun(p[0]) equals fun(x)See Equation 2

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

fun(q)

void fun(struct ABC **q);

fun(&q)

void fun(struct ABC ***q);

fun(*q)

void fun(struct ABC *q);

  • Step 1 : fun(*q) equals fun(p)

  • Step 2 : fun(p) equals fun(type_of(p))

  • Step 3 : fun(type_of(p)) equals fun(struct ABC *)

fun(q[0])

void fun(struct ABC *q);

  • Step 1 : fun(q[0]) equals fun(p)

  • Step 2 : fun(p) equals fun(type_of(p))

  • Step 3 : fun(type_of(p)) equals fun(struct ABC *)

fun(**q)

void fun(struct ABC x);

  • Step 1 : fun(**q) equals fun(x) See Equation 10

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

fun(*q[0])

void fun(struct ABC x);

  • Step 1 : fun(*q[0]) equals fun(x) See Equation 10

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

fun(q[0][0])

void fun(struct ABC x);

  • Step 1 : fun(q[0][0]) equals fun(x) See Equation 11

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

fun(r)

void fun(struct ABC ***r);

  • Step 1 : fun(r) equals fun(type_of(r))

  • Step 2 : fun(type_of(r)) equals fun(struct ABC ***)

fun(&r)

void fun(struct ABC ****s);

  • Step 1 : fun(&r) equals fun(type_of(&r))

  • Step 2 : fun(type_of(&r)) equals fun(struct ABC ****)

fun(*r)

void fun(struct ABC **q);

  • Step 1 : fun(*r) equals fun(q) See Equation 15

  • Step 2 : fun(q) equals fun(type_of(q))

  • Step 3 : fun(type_of(q)) equals fun(struct ABC **)

fun(r[0])

void fun(struct ABC **q);

  • Step 1 : fun(r[0]) equals fun(q) See Equation 15

  • Step 2 : fun(q) equals fun(type_of(q))

  • Step 3 : fun(type_of(q)) equals fun(struct ABC **)

fun(**r)

void fun(struct ABC *p);

  • Step 1 : fun(**r) equals fun(p) See Equation 19

  • Step 2 : fun(p) equals fun(type_of(p))

  • Step 3 : fun(type_of(p)) equals fun(struct ABC *)

fun(*r[0])

void fun(struct ABC *p);

  • Step 1 : fun(*r[0]) equals fun(p) See Equation 19

  • Step 2 : fun(p) equals fun(type_of(p))

  • Step 3 : fun(type_of(p)) equals fun(struct ABC *)

fun(r[0][0])

void fun(struct ABC *p);

  • Step 1 : fun(**r) equals fun(p) See Equation 19

  • Step 2 : fun(p) equals fun(type_of(p))

  • Step 3 : fun(type_of(p)) equals fun(struct ABC *)

fun(***r)

void fun(struct ABC x);

  • Step 1 : fun(***r) equals fun(x) See Equation 22

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

fun(**r[0])

void fun(struct ABC x);

  • Step 1 : fun(**r[0]) equals fun(x) See Equation 22

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

fun(r[0][0][0])

void fun(struct ABC x);

  • Step 1 : fun(r[0][0][0]) equals fun(x) See Equation 22

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(struct ABC)

4. Summary

#

?

Size in Bytes

Type

Address or Value

Function call

Function Prototype

x

Structure

12

struct ABC

Value

fun(x)

void fun(struct ABC x);

&x

Single Pointer

8

struct ABC *

Address

fun(&x)

void fun(struct ABC *p);

p

Single Pointer

8

struct ABC *

Address

fun(p)

void fun(struct ABC *p);

&p

Double Pointer

8

struct ABC **

Address

fun(&p)

void fun(struct ABC **q);

*p

Structure

12

struct ABC

Value

fun(*p)

void fun(struct ABC x);

p[0]

Structure

12

struct ABC

Value

fun(p[0])

void fun(struct ABC x);

q

Double Pointer

8

struct ABC **

Address

fun(q)

void fun(struct ABC **q);

&q

Triple Pointer

8

struct ABC ***

Address

fun(&q)

void fun(struct ABC ***r);

*q

Single Pointer

8

struct ABC *

Address

fun(*q)

void fun(struct ABC *p);

q[0]

Single Pointer

8

struct ABC *

Address

fun(q[0])

void fun(struct ABC *p);

**q

Structure

12

struct ABC

Value

fun(**q)

void fun(struct ABC x);

*q[0]

Structure

12

struct ABC

Value

fun(*q[0])

void fun(struct ABC x);

q[0][0]

Structure

12

struct ABC

Value

fun(q[0][0])

void fun(struct ABC x);

r

Triple Pointer

8

struct ABC ***

Address

fun(r)

void fun(struct ABC ***r);

&r

Fourth Pointer

8

struct ABC ****

Address

fun(&r)

void fun(struct ABC ****s);

*r

Double Pointer

8

struct ABC **

Address

fun(*r)

void fun(struct ABC **q);

r[0]

Double Pointer

8

struct ABC **

Address

fun(r[0])

void fun(struct ABC **q);

**r

Single Pointer

8

struct ABC *

Address

fun(**r)

void fun(struct ABC *p);

*r[0]

Single Pointer

8

struct ABC *

Address

fun(*r[0])

void fun(struct ABC *p);

r[0][0]

Single Pointer

8

struct ABC *

Address

fun(r[0][0])

void fun(struct ABC *p);

***r

Structure

12

struct ABC

Value

fun(***r)

void fun(struct ABC x);

**r[0]

Structure

12

struct ABC

Value

fun(**r[0])

void fun(struct ABC x);

r[0][0][0]

Structure

12

struct ABC

Value

fun(r[0][0][0])

void fun(struct ABC x);