Equations of Structure Triple Pointer ============================================================= In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the different ways of declarations ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to dervie equations ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the Properties of Variable ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What are the Properties of Expression ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Derive Equations for Triple Pointer ` * :ref:`Step 1 : Understand different methods to declare Triple Pointer ` * :ref:`Declaration 1 : Structure x, Single Pointer p, Double Pointer q, Triple Pointer r are declared and assigned in same line ` * :ref:`Declaration 2 : Structure x, Single Pointer p, Double Pointer q, Triple Pointer r are declared and assigned in separate lines ` * :ref:`Declaration 3 : Structure x, Single Pointer p, Double Pointer q are declared in one line and assigned in another line ` * :ref:`Table of Declarations for Triple Pointer ` * :ref:`Step 2 : Split pointer declarations and assignments into separate lines ` * :ref:`Step 3 : Derive Equations ` * :ref:`Step 4 : Apply above equations to analyse C statements ` * :ref:`Table of Equations for Single Pointer ` * :ref:`Table of Equations for Double Pointer ` * :ref:`Table of Equations for Triple Pointer ` * :ref:`Properties of a variable ` * :ref:`Property 1 : Type ` * :ref:`Property 2 : sizeof() ` * :ref:`Property 3 : Scope, Lifetime and Memory of a variable ` * :ref:`Properties of Expressions ` * :ref:`Table of Expressions ` * :ref:`Table of Size (for Expressions) ` * :ref:`Table of Types (for Expressions) ` * :ref:`Table of Address/Value (for Expression) ` * :ref:`Table of Function Prototype (for Expression) ` * :ref:`Summary ` 1. Equations of Triple Pointer *********************************************************************** .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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 ? .. _variable_and_ptr_tp_struct_ptr_ex_1: .. tab-set:: .. tab-item:: Derive Equations for Triple Pointer .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow You can derive equations looking at C declarations ! .. _variable_and_ptr_tp_struct_ptr_ex_2: .. tab-set:: .. tab-item:: Step 1 : Understand different methods to declare Triple Pointer .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow There are many methods in C, using which a Triple pointer can be declared. See below .. _variable_and_ptr_tp_struct_ptr_ex_3: .. tab-set:: .. tab-item:: Declaration 1 : Structure ``x``, Single Pointer ``p``, Double Pointer ``q``, Triple Pointer ``r`` are declared and assigned in same line .. code-block:: c :linenos: :emphasize-lines: 12 #include struct ABC { int a; int b; int c; }; int main(void) { struct ABC x = {.a = 1, .b = 2, .c = 3}, *p = &x, **q = &p, ***r = &q; printf("x.a = %d\n", x.a); printf("x.b = %d\n", x.b); printf("x.c = %d\n", x.c); printf("(***r).a = %d\n", (***r).a); printf("(***r).b = %d\n", (***r).b); printf("(***r).c = %d\n", (***r).c); printf("(**r)->a = %d\n", (**r)->a); printf("(**r)->b = %d\n", (**r)->b); printf("(**r)->c = %d\n", (**r)->c); printf("(*r)[0][0].a = %d\n", (*r)[0][0].a); printf("(*r)[0][0].b = %d\n", (*r)[0][0].b); printf("(*r)[0][0].c = %d\n", (*r)[0][0].c); printf("(**r)[0].a = %d\n", (**r)[0].a); printf("(**r)[0].b = %d\n", (**r)[0].b); printf("(**r)[0].c = %d\n", (**r)[0].c); printf("r[0][0][0].a = %d\n", r[0][0][0].a); printf("r[0][0][0].b = %d\n", r[0][0][0].b); printf("r[0][0][0].c = %d\n", r[0][0][0].c); return 0; } * Output is as below .. code-block:: c 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 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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`` .. _variable_and_ptr_tp_struct_ptr_ex_4: .. tab-set:: .. tab-item:: Declaration 2 : Structure ``x``, Single Pointer ``p``, Double Pointer ``q``, Triple Pointer ``r`` are declared and assigned in separate lines .. code-block:: c :linenos: :emphasize-lines: 12, 14, 16, 18 #include struct ABC { int a; int b; int c; }; int main(void) { struct ABC x = {.a = 1, .b = 2, .c = 3}; struct ABC *p = &x; struct ABC **q = &p; struct ABC ***r = &q; printf("x.a = %d\n", x.a); printf("x.b = %d\n", x.b); printf("x.c = %d\n", x.c); printf("(***r).a = %d\n", (***r).a); printf("(***r).b = %d\n", (***r).b); printf("(***r).c = %d\n", (***r).c); printf("(**r)->a = %d\n", (**r)->a); printf("(**r)->b = %d\n", (**r)->b); printf("(**r)->c = %d\n", (**r)->c); printf("(*r)[0][0].a = %d\n", (*r)[0][0].a); printf("(*r)[0][0].b = %d\n", (*r)[0][0].b); printf("(*r)[0][0].c = %d\n", (*r)[0][0].c); printf("(**r)[0].a = %d\n", (**r)[0].a); printf("(**r)[0].b = %d\n", (**r)[0].b); printf("(**r)[0].c = %d\n", (**r)[0].c); printf("r[0][0][0].a = %d\n", r[0][0][0].a); printf("r[0][0][0].b = %d\n", r[0][0][0].b); printf("r[0][0][0].c = %d\n", r[0][0][0].c); return 0; } * Output is as below .. code-block:: c 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 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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`` .. _variable_and_ptr_tp_struct_ptr_ex_5: .. _variable_and_ptr_tp_struct_ptr_declaration_3: .. tab-set:: .. tab-item:: Declaration 3 : Structure ``x``, Single Pointer ``p``, Double Pointer ``q`` are declared in one line and assigned in another line .. code-block:: c :linenos: :emphasize-lines: 12, 14, 16, 18 #include struct ABC { int a; int b; int c; }; int main(void) { 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; printf("x.a = %d\n", x.a); printf("x.b = %d\n", x.b); printf("x.c = %d\n", x.c); printf("(***r).a = %d\n", (***r).a); printf("(***r).b = %d\n", (***r).b); printf("(***r).c = %d\n", (***r).c); printf("(**r)->a = %d\n", (**r)->a); printf("(**r)->b = %d\n", (**r)->b); printf("(**r)->c = %d\n", (**r)->c); printf("(*r)[0][0].a = %d\n", (*r)[0][0].a); printf("(*r)[0][0].b = %d\n", (*r)[0][0].b); printf("(*r)[0][0].c = %d\n", (*r)[0][0].c); printf("(**r)[0].a = %d\n", (**r)[0].a); printf("(**r)[0].b = %d\n", (**r)[0].b); printf("(**r)[0].c = %d\n", (**r)[0].c); printf("r[0][0][0].a = %d\n", r[0][0][0].a); printf("r[0][0][0].b = %d\n", r[0][0][0].b); printf("r[0][0][0].c = %d\n", r[0][0][0].c); return 0; } * Output is as below .. code-block:: c 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 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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`` .. _variable_and_ptr_tp_struct_ptr_ex_6: .. tab-set:: .. tab-item:: Table of Declarations for Triple Pointer ========== =========================================================== ==================================================================================================== 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; Structure x, Single Pointer p, Double Pointer q, Triple Pointer r are declared and assigned in separate lines * struct ABC \*p = &x; * struct ABC \*\*q = &p; * struct ABC \*\*\*r = &q; Decl 3 * struct ABC x; Structure x, Single Pointer p, Double Pointer q, Triple Pointer r are declared in one line and assigned in another line * struct ABC \*p; * struct ABC \*\*q; * struct ABC \*\*\*r; * x.a = 1; * x.b = 2; * x.c = 3; * p = &x; * q = &p; * r = &q; ========== =========================================================== ==================================================================================================== .. _variable_and_ptr_tp_struct_ptr_ex_7: .. tab-set:: .. tab-item:: Step 2 : Split pointer declarations and assignments into separate lines .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * 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 :ref:`Declaration 3 ` .. _variable_and_ptr_tp_struct_ptr_ex_8: .. tab-set:: .. tab-item:: Step 3 : Derive Equations .. tab-set:: .. tab-item:: Derive Equations of Single Pointer .. _variable_and_ptr_tp_struct_ptr_eq_1: * Equation 1 : Obtained from ``Step 2`` .. code-block:: c p = &x; .. _variable_and_ptr_tp_struct_ptr_eq_2: * Equation 2 : Move ``&`` to the left of First Equation. It turns in to ``*`` .. code-block:: c *p = x; .. _variable_and_ptr_tp_struct_ptr_eq_3: * Equation 3 : * and [0] can be used interchangeably. Hence ``*p`` and ``p[0]`` are one and the same ! .. code-block:: c p[0] = x; .. _variable_and_ptr_tp_struct_ptr_eq_4: * Equation 4 : From Equation 2, Equation 3, we can derive that ``x``, ``*p``, ``p[0]`` all are same ! .. code-block:: c *p = p[0] = x; .. tab-set:: .. tab-item:: Derive Equations of Double Pointer .. _variable_and_ptr_tp_struct_ptr_eq_5: * Equation 5 : Obtained from ``Step 2`` .. code-block:: c q = &p; .. _variable_and_ptr_tp_struct_ptr_eq_6: * Equation 6 : Move & to the left of First Equation. It turns in to \* .. code-block:: c *q = p; .. _variable_and_ptr_tp_struct_ptr_eq_7: * Equation 7 : \* and [0] can be used interchangeably. Hence \*q and q[0] are one and the same ! .. code-block:: c q[0] = p; .. _variable_and_ptr_tp_struct_ptr_eq_8: * Equation 8 : From Equation 6, Equation 7, we can derive that ``p``, ``*q``, ``q[0]`` all are same ! .. code-block:: c *q = q[0] = p; .. _variable_and_ptr_tp_struct_ptr_eq_9: * Equation 9 : From Equation 1, we know ``p = &x``. Hence Replace ``p`` with ``&x`` in Equation 8 .. code-block:: c *q = q[0] = &x; .. _variable_and_ptr_tp_struct_ptr_eq_10: * Equation 10 : Move & to the left of Equation 9. It turns in to * .. code-block:: c **q = *q[0] = x; .. _variable_and_ptr_tp_struct_ptr_eq_11: * Equation 11 : \* and [0] can be used interchangeably .. code-block:: c q[0][0] = x; .. tab-set:: .. tab-item:: Derive Equations of Triple Pointer .. _variable_and_ptr_tp_struct_ptr_eq_12: * Equation 12 : Obtained from ``Step 2`` .. code-block:: c r = &q; .. _variable_and_ptr_tp_struct_ptr_eq_13: * Equation 13 : Move & to the left of First Equation. It turns in to \* .. code-block:: c *r = q; .. _variable_and_ptr_tp_struct_ptr_eq_14: * Equation 14 : \* and [0] can be used interchangeably. Hence \*r and r[0] are one and the same ! .. code-block:: c r[0] = q; .. _variable_and_ptr_tp_struct_ptr_eq_15: * Equation 15 : From Equation 13, Equation 14, we can derive that ``q``, ``*r``, ``r[0]`` all are same ! .. code-block:: c *r = r[0] = q; .. _variable_and_ptr_tp_struct_ptr_eq_16: * Equation 16 : From Equation 5, we know ``q = &p``. Hence Replace ``q`` with ``&p`` in Equation 15 .. code-block:: c *r = r[0] = &p; .. _variable_and_ptr_tp_struct_ptr_eq_17: * Equation 17 : Move & to the left of Equation 16. It turns in to * .. code-block:: c **r = *r[0] = p; .. _variable_and_ptr_tp_struct_ptr_eq_18: * Equation 18 : \* and [0] can be used interchangeably .. code-block:: c r[0][0] = p; .. _variable_and_ptr_tp_struct_ptr_eq_19: * Equation 19 : Comparing Equation 17, 18 we can conclude ``**r``, ``*r[0]``, ``r[0][0]`` and ``p`` all are same ! .. code-block:: c **r = *r[0] = r[0][0] = p; .. _variable_and_ptr_tp_struct_ptr_eq_20: * Equation 20 : From Equation 1, ``p = &x``. Hence replace ``p`` with ``&x`` in Equation 19 .. code-block:: c **r = *r[0] = r[0][0] = &x; .. _variable_and_ptr_tp_struct_ptr_eq_21: * Equation 21 : Move & to the left of Equation 16. It turns in to * .. code-block:: c ***r = **r[0] = *r[0][0] = x; .. _variable_and_ptr_tp_struct_ptr_eq_22: * Equation 22 : \* and [0] can be used interchangeably .. code-block:: c ***r = **r[0] = r[0][0][0] = x; .. _variable_and_ptr_tp_struct_ptr_ex_10: .. tab-set:: .. tab-item:: Table of Equations for Single Pointer =========== ================== =========================================================================== 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 =========== ================== =========================================================================== .. _variable_and_ptr_tp_struct_ptr_ex_22: .. tab-set:: .. tab-item:: Table of Equations for Double Pointer =========== ================== =========================================================================== 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 =========== ================== =========================================================================== .. _variable_and_ptr_tp_struct_ptr_ex_23: .. tab-set:: .. tab-item:: Table of Equations for Triple Pointer =========== ==================================== ============================================================================================================ 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 =========== ==================================== ============================================================================================================ .. _variable_and_ptr_tp_struct_ptr_ex_9: .. tab-set:: .. tab-item:: Step 4 : Apply above equations to analyse C statements .. tab-set:: .. tab-item:: Example 1 : :ref:`See Equation 2 ` : Changing ``*p`` changes the value of ``x`` .. code-block:: c (*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 .. code-block:: c (*p).a = 10 (*p).b = 20 (*p).c = 30 .. tab-set:: .. tab-item:: Example 2 : :ref:`See Equation 3 ` : Changing ``p[0]`` changes the value of ``x`` .. code-block:: c 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 .. code-block:: c p[0].a = 10 p[0].b = 20 p[0].c = 30 .. tab-set:: .. tab-item:: Example 3 : :ref:`See Equation 4 ` : Changing ``x`` changes the value of ``*p`` and ``p[0]`` .. code-block:: c 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 .. code-block:: c x.a = 100 (*p).a = 100 p[0].a = 100 p->a = 100 .. tab-set:: .. tab-item:: Example 4 : Use "p->" notation of pointer to change contents of variable "x" .. code-block:: c 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 .. code-block:: c p->a = 10 p->b = 20 p->c = 30 .. tab-set:: .. tab-item:: Example 5 : :ref:`See Equation 10 ` : Changing ``**q`` changes the value of ``x`` .. code-block:: c (**q).a = 100; printf("x.a = %d, (**q).a = %d\n", x.a, (**q).a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(**q).a`` = 100 .. tab-set:: .. tab-item:: Example 6 : :ref:`See Equation 10 ` : Changing ``*q[0]`` changes the value of ``x`` .. code-block:: c (*q)[0].a = 100; printf("x.a = %d, (*q)[0].a = %d\n", x.a, (*q)[0].a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(*q)[0].a`` = 100 .. tab-set:: .. tab-item:: Example 7 : :ref:`See Equation 11 ` : Changing ``q[0][0]`` changes the value of ``x`` .. code-block:: c q[0][0].a = 100; printf("x.a = %d, q[0][0].a = %d\n", x.a, q[0][0].a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``q[0][0].a`` = 100 .. tab-set:: .. tab-item:: Example 8 : :ref:`See Equation 6 ` : ``*q``, ``p`` are synonyms. Means chaning ``*q`` also changes ``p`` .. code-block:: c 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); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``(*p).a`` = 100, ``p[0].a`` = 100, ``(**q).a`` = 100 ``(*p).a`` = 200, ``p[0].a`` = 200, ``(**q).a`` = 200 .. tab-set:: .. tab-item:: Example 9 : :ref:`See Equation 7 ` : ``q[0]``, ``p`` are synonyms. Means chaning ``q[0]`` also changes ``p`` .. code-block:: c 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); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``(*p).a`` = 100, ``p[0].a`` = 100, ``(**q).a`` = 100 .. tab-set:: .. tab-item:: Example 10 : :ref:`See Equation 22 ` : Changing ``***r`` changes the value of ``x`` .. code-block:: c (***r).a = 100; printf("x.a = %d, (***r).a = %d\n", x.a, (***r).a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(***r).a`` = 100 .. tab-set:: .. tab-item:: Example 11 : :ref:`See Equation 22 ` : Changing ``**r[0]`` changes the value of ``x`` .. code-block:: c (**r)[0].a = 100; printf("x.a = %d, (**r[0]).a = %d\n", x.a, (**r[0]).a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(**r[0]).a`` = 100 .. tab-set:: .. tab-item:: Example 12 : :ref:`See Equation 21 ` : Changing ``*r[0][0]`` changes the value of ``x`` .. code-block:: c (*r)[0][0].a = 100; printf("x.a = %d, (*r)[0][0].a = %d\n", x.a, (*r)[0][0].a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(*r)[0][0].a`` = 100 .. tab-set:: .. tab-item:: Example 13 : :ref:`See Equation 22 ` : Changing ``r[0][0][0]`` changes the value of ``x`` .. code-block:: c r[0][0][0].a = 100; printf("x.a = %d, r[0][0][0].a = %d\n", x.a, r[0][0][0].a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``r[0][0][0].a`` = 100 .. tab-set:: .. tab-item:: Example 14 : Changing ``(**r)->`` changes the value of ``x`` .. code-block:: c (**r)->a = 100; printf("x.a = %d, (**r)->a = %d\n", x.a, (**r)->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(**r)->a`` = 100 .. tab-set:: .. tab-item:: Example 15 : Changing ``(*r[0])->`` changes the value of ``x`` .. code-block:: c (*r[0])->a = 100; printf("x.a = %d, (*r[0])->a = %d\n", x.a, (*r[0])->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(*r[0])->a`` = 100 .. tab-set:: .. tab-item:: Example 16 : Changing ``(r[0][0])->`` changes the value of ``x`` .. code-block:: c (r[0][0])->a = 100; printf("x.a = %d, (r[0][0])->a = %d\n", x.a, (r[0][0])->a); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Output : ``x.a`` = 100, ``(r[0][0])->a`` = 100 .. tab-set:: .. tab-item:: Example 17 : :ref:`See Equation 19 ` : Changing ``**r`` changes the value of ``p`` .. code-block:: c 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); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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 .. tab-set:: .. tab-item:: Example 18 : :ref:`See Equation 19 ` : Changing ``*r[0]`` changes the value of ``p`` .. code-block:: c 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); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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 .. tab-set:: .. tab-item:: Example 19 : :ref:`See Equation 19 ` : Changing ``r[0][0]`` changes the value of ``p`` .. code-block:: c 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); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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 .. tab-set:: .. tab-item:: Example 20 : :ref:`See Equation 15 ` : Changing ``*r`` changes the value of ``q`` .. code-block:: c 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); .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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 .. tab-set:: .. tab-item:: Step 5 : Complete program .. code-block:: c :linenos: #include struct ABC { int a; int b; int c; }; int main(void) { struct ABC x; struct ABC *p; struct ABC **q; struct ABC ***r; x.a = 10; p = &x; q = &p; r = &q; 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); x.a = 20; 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); (*p).a = 30; 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); p[0].a = 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); (**q).a = 50; 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); (***r).a = 60; 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); return 0; } * Output is as below .. code-block:: c 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 .. _variable_and_ptr_tp_struct_ptr_ex_11: 2. Properties of a variable *********************************************************************** .. tab-set:: .. tab-item:: Properties of a variable .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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 ? .. _variable_and_ptr_tp_struct_ptr_ex_12: .. tab-set:: .. tab-item:: Property 1 : Type .. _variable_and_ptr_tp_struct_ptr_property_1_1: .. tab-set:: .. tab-item:: Property 1.1 : type_of(variable) .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 7 struct ABC x; struct ABC *p; struct ABC **q; struct ABC ***r; p = &x; q = &p; r = &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 =========== ===================== ============================================================================== .. _variable_and_ptr_tp_struct_ptr_property_1_2: .. tab-set:: .. tab-item:: Property 1.2 : type_of(&variable) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Adress of variable must be stored in next level pointer type always .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 7 struct ABC x; struct ABC *p; struct ABC **q; struct ABC ***r; p = &x; q = &p; r = &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 ****`` =========== ========================= ========================================================================================== .. _variable_and_ptr_tp_struct_ptr_ex_13: .. tab-set:: .. tab-item:: Property 2 : sizeof() .. tab-set:: .. tab-item:: Property 2.1 : sizeof() of basic types ======================= ======= Sizeof(type) Size ======================= ======= sizeof(char) 1 Byte sizeof(int) 4 Bytes sizeof(float) 4 Bytes sizeof(double) 8 Bytes ======================= ======= .. tab-set:: .. tab-item:: Property 2.2 : sizeof() of pointer types .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``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., ========================= ======= .. _variable_and_ptr_tp_struct_ptr_property_2_3: .. tab-set:: .. tab-item:: Property 2.3 : sizeof(&variable) - sizeof address of variable .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``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;`` ======================= ======= ======================== .. _variable_and_ptr_tp_struct_ptr_property_2_4: .. tab-set:: .. tab-item:: Property 2.4 : sizeof() of variable .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow ``sizeof(variable)`` equals ``sizeof(typeof(variable))`` .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5, 7 struct ABC x; struct ABC *p; struct ABC **q; struct ABC ***r; p = &x; q = &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`` ======================= ======== ================================================================================ .. _variable_and_ptr_tp_struct_ptr_ex_14: .. tab-set:: .. tab-item:: Property 3 : Scope, Lifetime and Memory of a variable * Global Scope and Lifetime. * Local Scope and Lifetime .. _variable_and_ptr_tp_struct_ptr_ex_15: 3. Properties of Expressions *********************************************************************** .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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) .. tab-set:: .. tab-item:: Properties of Expressions .. tab-set:: .. tab-item:: Let us write-down expression tables for below code snippet .. code-block:: c :linenos: :emphasize-lines: 1, 3, 5 struct ABC x; struct ABC *p; struct ABC **q; struct ABC ***r; p = &x; q = &p; r = &q; .. _variable_and_ptr_tp_struct_ptr_ex_16: .. tab-set:: .. tab-item:: Table of Expressions ========== ================================================================================================================================= 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`` is a structure, because ``*p = x``. :ref:`See Equation 2 ` p[0] * ``p[0]`` is a structure, because ``p[0] = x``. :ref:`See Equation 3 ` 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`` holds the address of structure x. :ref:`See Equation 9 ` * ``*q`` is a single pointer. ``*q = p``. :ref:`See Equation 8 ` q[0] * ``q[0]`` holds the address of structure x. :ref:`See Equation 9 ` * ``q[0]`` is a single pointer. ``*q = p``. :ref:`See Equation 8 ` \*\*q * ``**q`` is a structure. :ref:`See Equation 10 ` \*q[0] * ``*q[0]`` is a structure. :ref:`See Equation 10 ` q[0][0] * ``q[0][0]`` is a structure. :ref:`See Equation 11 ` r * ``r`` is a triple pointer and holds the address of a double pointer &r * ``&r`` is a fourth pointer \*r * ``*r`` is a double pointer. :ref:`See Equation 15 ` * ``*r`` holds the address of a single pointer. :ref:`See Equation 16 ` r[0] * ``*r`` is a double pointer. :ref:`See Equation 15 ` * ``*r`` holds the address of a single pointer. :ref:`See Equation 16 ` \*\*r * ``**r`` is a single pointer. :ref:`See Equation 17 ` * ``**r`` holds the address of a structure. :ref:`See Equation 20 ` \*r[0] * ``*r[0]`` is a single pointer. :ref:`See Equation 17 ` * ``*r[0]`` holds the address of a structure. :ref:`See Equation 20 ` r[0][0] * ``r[0][0]`` is a single pointer. :ref:`See Equation 19 ` * ``r[0][0]`` holds the address of a structure. :ref:`See Equation 20 ` \*\*\*r * ``***r`` is a structure. :ref:`See Equation 22 ` \*\*r[0] * ``**r[0]`` is a structure. :ref:`See Equation 22 ` r[0][0][0] * ``r[0][0][0]`` is a structure. :ref:`See Equation 22 ` ========== ================================================================================================================================= .. _variable_and_ptr_tp_struct_ptr_ex_17: .. tab-set:: .. tab-item:: Table of Size (for Expressions) ================== ======== ============================================================================================================================================================= Expression Size Description ================== ======== ============================================================================================================================================================= sizeof(x) 12 Bytes * :ref:`See Property 2.4 ` sizeof(&x) 8 Bytes * :ref:`See Property 2.3 ` sizeof(p) 8 Bytes * :ref:`See Property 2.4 ` sizeof(&p) 8 Bytes * :ref:`See Property 2.3 ` sizeof(\*p) 12 Bytes * Step 1 : ``sizeof(*p)`` equals ``sizeof(x)`` :ref:`See Equation 2 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`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`` :ref:`See Equation 3 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC)`` equals 12 Bytes sizeof(q) 8 Bytes * :ref:`See Property 2.4 ` sizeof(&q) 8 Bytes * :ref:`See Property 2.3 ` sizeof(\*q) 8 Bytes * Step 1 : ``sizeof(*q)`` equals ``sizeof(p)``. :ref:`See Equation 8 ` * Step 2 : ``sizeof(p)``, equals ``sizeof(type_of(p))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(p))`` equals ``sizeof(struct ABC *)`` :ref:`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)`` :ref:`See Equation 8 ` * Step 2 : ``sizeof(p)`` equals ``sizeof(type_of(p))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(p))`` equals ``sizeof(struct ABC *)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC *)`` equals 8 Bytes sizeof(\*\*q) 12 Bytes * Step 1 : ``sizeof(**q)`` equals ``sizeof(x)`` :ref:`See Equation 10 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC)`` equals 12 Bytes sizeof(\*q[0]) 12 Bytes * Step 1 : ``sizeof(*q[0])`` equals ``sizeof(x)`` :ref:`See Equation 10 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC)`` equals 12 Bytes sizeof(q[0][0]) 12 Bytes * Step 1 : ``sizeof(q[0][0])`` equals ``sizeof(x)`` :ref:`See Equation 11 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC)`` equals 12 Bytes sizeof(r) 8 Bytes * :ref:`See Property 2.4 ` sizeof(&r) 8 Bytes * :ref:`See Property 2.3 ` sizeof(\*r) 8 Bytes * Step 1 : ``sizeof(*r)`` equals ``sizeof(q)``. :ref:`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)``. :ref:`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)``. :ref:`See Equation 19 ` * Step 2 : ``sizeof(p)`` equals ``sizeof(type_of(p))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(p))`` equals ``sizeof(struct ABC *)`` :ref:`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)``. :ref:`See Equation 19 ` * Step 2 : ``sizeof(p)`` equals ``sizeof(type_of(p))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(p))`` equals ``sizeof(struct ABC *)`` :ref:`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)``. :ref:`See Equation 19 ` * Step 2 : ``sizeof(p)`` equals ``sizeof(type_of(p))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(p))`` equals ``sizeof(struct ABC *)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC *)`` equals 8 Bytes sizeof(\*\*\*r) 12 Bytes * Step 1 : ``sizeof(***r)`` equals ``sizeof(x)``. :ref:`See Equation 22 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC)`` equals 12 Bytes. :ref:`See Equation 22 ` sizeof(\*\*r[0]) 12 Bytes * Step 1 : ``sizeof(**r[0])`` equals ``sizeof(x)``. :ref:`See Equation 22 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC)`` equals 12 Bytes. :ref:`See Equation 22 ` sizeof(r[0][0][0]) 12 Bytes * Step 1 : ``sizeof(r[0][0][0])`` equals ``sizeof(x)``. :ref:`See Equation 22 ` * Step 2 : ``sizeof(x)`` equals ``sizeof(type_of(x))`` :ref:`See Property 2.4 ` * Step 3 : ``sizeof(type_of(x))`` equals ``sizeof(struct ABC)`` :ref:`See Property 1.1 ` * Step 4 : ``sizeof(struct ABC)`` equals 12 Bytes ================== ======== ============================================================================================================================================================= .. _variable_and_ptr_tp_struct_ptr_ex_18: .. tab-set:: .. tab-item:: Table of Types (for Expressions) ==================== ======================================== ================================================================================================================================================== Expression Type Description ==================== ======================================== ================================================================================================================================================== type_of(x) struct ABC * :ref:`See Property 1.1 ` type_of(&x) struct ABC \* * :ref:`See Property 1.2 ` type_of(p) struct ABC \* * :ref:`See Property 1.1 ` type_of(&p) struct ABC \*\* * :ref:`See Property 1.2 ` type_of(\*p) struct ABC * Step 1 : ``type_of(*p)`` equals ``type_of(x)``, because ``*p = x``. :ref:`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``. :ref:`See Equation 3 ` * Step 2 : ``type_of(x)`` equals ``struct ABC`` type_of(q) struct ABC \*\* * :ref:`See Property 1.1 ` type_of(&q) struct ABC \*\*\* * :ref:`See Property 1.2 ` type_of(\*q) struct ABC \* * Step 1 : ``type_of(*q)`` equals ``type_of(p)``. :ref:`See Equation 8 ` * Step 2 : ``type_of(p)``, equals ``struct ABC *`` :ref:`See Property 1.1 ` type_of(q[0]) struct ABC \* * Step 1 : ``type_of(q[0])`` equals ``type_of(p)`` :ref:`See Equation 8 ` * Step 2 : ``type_of(p)`` equals ``struct ABC *`` :ref:`See Property 1.1 ` type_of(\*\*q) struct ABC * Step 1 : ``type_of(**q)`` equals ``type_of(x)`` :ref:`See Equation 10 ` * Step 2 : ``type_of(x)`` equals ``struct ABC`` :ref:`See Property 1.1 ` type_of(\*q[0]) struct ABC * Step 1 : ``type_of(*q[0])`` equals ``type_of(x)`` :ref:`See Equation 10 ` * Step 2 : ``type_of(x)`` equals ``struct ABC`` :ref:`See Property 1.1 ` type_of(q[0][0]) struct ABC * Step 1 : ``type_of(q[0][0])`` equals ``type_of(x)`` :ref:`See Equation 11 ` * Step 2 : ``type_of(x)`` equals ``struct ABC`` :ref:`See Property 1.1 ` type_of(r) struct ABC \*\*\* * :ref:`See Property 2.4 ` type_of(&r) struct ABC \*\*\*\* * :ref:`See Property 2.3 ` type_of(\*r) struct ABC \*\* * Step 1 : ``type_of(*r)`` equals ``type_of(q)``. :ref:`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)``. :ref:`See Equation 15 ` * Step 2 : ``type_of(q)`` equals ``struct ABC **`` type_of(\*\*r) struct ABC \* * Step 1 : ``type_of(**r)`` equals ``type_of(p)``. :ref:`See Equation 19 ` * Step 2 : ``type_of(type_of(p))`` equals ``struct ABC *`` :ref:`See Property 1.1 ` type_of(\*r[0]) struct ABC \* * Step 1 : ``type_of(*r[0])`` equals ``type_of(p)``. :ref:`See Equation 19 ` * Step 2 : ``type_of(type_of(p))`` equals ``struct ABC *`` :ref:`See Property 1.1 ` type_of(r[0][0]) struct ABC \* * Step 1 : ``type_of(r[0][0])`` equals ``type_of(p)``. :ref:`See Equation 19 ` * Step 2 : ``type_of(type_of(p))`` equals ``struct ABC *`` :ref:`See Property 1.1 ` type_of(\*\*\*r) struct ABC * Step 1 : ``type_of(***r)`` equals ``type_of(x)``. :ref:`See Equation 22 ` * Step 2 : ``type_of(x)`` equals ``struct ABC`` :ref:`See Property 2.4 ` type_of(\*\*r[0]) struct ABC * step 1 : ``type_of(**r[0])`` equals ``type_of(x)``. :ref:`see equation 22 ` * step 2 : ``type_of(x)`` equals ``struct ABC`` :ref:`see property 2.4 ` type_of(r[0][0][0]) struct ABC * step 1 : ``type_of(r[0][0][0])`` equals ``type_of(x)``. :ref:`see equation 22 ` * step 2 : ``type_of(x)`` equals ``struct ABC`` :ref:`see property 2.4 ` ==================== ======================================== ================================================================================================================================================== .. _variable_and_ptr_tp_struct_ptr_ex_19: .. tab-set:: .. tab-item:: Table of Address/Value (for Expression) ========== ============== ============================================================================================================== 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`` :ref:`See Equation 1 ` * Step 2 : & operator indicates address &p Address * & operator indicates address \*p Value * Step 1 : ``*p`` is a structure. ``*p = x`` :ref:`See Equation 2 ` * Step 2 : Hence ``*p`` is a value p[0] Value * Step 1 : ``p[0]`` is a structure. ``p[0] = x`` :ref:`See Equation 3 ` * Step 2 : Hence ``p[0]`` is a value q Address * Step 1 : ``q = &p`` :ref:`See Equation 5 ` * Step 2 : & operator indicates address &q Address * & operator indicates address \*q Address * Step 1 : ``*q = p`` :ref:`See Equation 6 ` * Step 2 : ``p = &x`` * Step 3 : & operator indicates address q[0] Address * Step 1 : ``q[0] = p`` :ref:`See Equation 7 ` * Step 2 : ``p = &x`` * Step 3 : & operator indicates address \*\*q Value * Step 1 : ``**q = x`` :ref:`See Equation 10 ` * Step 2 : ``x`` is a structure * Step 3 : ``**q`` is a Value \*q[0] Value * Step 1 : ``*q[0] = x`` :ref:`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`` :ref:`See Equation 11 ` * Step 2 : ``x`` is a structure * Step 3 : ``q[0][0]`` is a Value r Address * Step 1 : ``r = &q`` :ref:`See Equation 12 ` * Step 2 : & operator indicates address &r Address * Step 1 : & operator indicates address \*r Address * Step 1 : ``*r = q`` :ref:`See Equation 15 ` * Step 2 : ``*r = &p`` :ref:`See Equation 16 ` * Step 3 : & operator indicates address r[0] Address * Step 1 : ``*r = q`` :ref:`See Equation 15 ` * Step 2 : ``*r = &p`` :ref:`See Equation 16 ` * Step 3 : & operator indicates address \*\*r Address * Step 1 : ``**r = p`` :ref:`See Equation 19 ` * Step 2 : ``**r = &x`` :ref:`See Equation 20 ` * Step 3 : & operator indicates address \*r[0] Address * Step 1 : ``*r[0] = p`` :ref:`See Equation 19 ` * Step 2 : ``*r[0] = &x`` :ref:`See Equation 20 ` * Step 3 : & operator indicates address r[0][0] Address * Step 1 : ``r[0][0] = p`` :ref:`See Equation 19 ` * Step 2 : ``r[0][0] = &x`` :ref:`See Equation 20 ` * Step 3 : & operator indicates address \*\*\*r Value * Step 1 : ``***r = x`` :ref:`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`` :ref:`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`` :ref:`See Equation 22 ` * Step 2 : ``x`` is a structure * Step 3 : ``r[0][0][0]`` is a Value ========== ============== ============================================================================================================== .. _variable_and_ptr_tp_struct_ptr_ex_20: .. tab-set:: .. tab-item:: Table of Function Prototype (for Expression) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow 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); * :ref:`See Property 1.1 ` fun(&x) void fun(struct ABC \*p); * :ref:`See Property 1.2 ` fun(p) void fun(struct ABC \*p); * :ref:`See Property 1.1 ` fun(&p) void fun(struct ABC \*\*p); * :ref:`See Property 1.2 ` fun(\*p) void fun(struct ABC x); * Step 1 : ``fun(*p)`` equals ``fun(x)``:ref:`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)``:ref:`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); * :ref:`See Property 1.1 ` fun(&q) void fun(struct ABC \*\*\*q); * :ref:`See Property 1.2 ` 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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`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)`` :ref:`See Equation 22 ` * Step 2 : ``fun(x)`` equals ``fun(type_of(x))`` * Step 3 : ``fun(type_of(x))`` equals ``fun(struct ABC)`` =============== ============================================== ========================================================================================================================= .. _variable_and_ptr_tp_struct_ptr_ex_21: 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); ========== ============== ============= ========================= ================ =============== =========================================== .. card:: See Also * :doc:`sp_struct_ptr` * :doc:`tp_struct_ptr` .. card:: See Also * Current Module * :doc:`variable_and_ptr` * Next Module * :doc:`../array_n_ptrs/array_n_ptrs` * Other Modules * :doc:`../malloc_ptr/malloc_ptr` * :doc:`../typecasting_n_ptr/typecasting_n_ptr` * :doc:`../funcs_n_ptrs/funcs_n_ptrs` * :doc:`../memcpy_ptr/memcpy_ptr` * :doc:`../const_ptr/const_ptr` * :doc:`../void_ptr/void_ptr` * :doc:`../array_of_ptr/array_of_ptr` * :doc:`../ptr_to_array/ptr_to_array` * :doc:`../function_ptr/function_ptr` * :doc:`../pre_incr_ptr/pre_incr_ptr` * :doc:`../post_incr_ptr/post_incr_ptr` * :doc:`../pre_decr_ptr/pre_decr_ptr` * :doc:`../post_decr_ptr/post_decr_ptr`