C-Array-Pointer-equations -------------------------- +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule | Description | Example | | | | | | | | | +==========+============================================================================================================+=====================================+ | Rule 1 | **A pointer can hold the address of a static global variable** | .. literalinclude:: ptr_rule_1.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 2 | **A pointer can hold the address of a non-static global variable** | .. literalinclude:: ptr_rule_2.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 3 | **A pointer can hold the address of a static local variable** | .. literalinclude:: ptr_rule_3.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 4 | **A pointer can hold the address of a local variable stored on stack** | .. literalinclude:: ptr_rule_4.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 5 | **A pointer can hold the address of a memory block allocated dynamically by malloc, calloc, realloc** | .. literalinclude:: ptr_rule_5_e1.py| | | | .. literalinclude:: ptr_rule_5_e2.py| +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 6 | **More than one pointer can point to same memory location** | .. literalinclude:: ptr_rule_6.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 7 | **[ ] and \* can be interchanged in arrays (Single dimension, Double dimension, etc., )** | .. literalinclude:: ptr_rule_7.py | | | * Rule defined for int x[], char x[], float x[], double x[], struct ABC x[], union ABC x[] | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 8 | **[ ] and \* can be interchanged in pointers (Single pointer, Double pointer, etc., )** | .. literalinclude:: ptr_rule_8.py | | | * Rule defined for int \*x, char \*x, float \*x, double \*x, struct ABC \*x, union ABC \*x | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 9 | **\*ptr is always equal to ptr[0]** | .. literalinclude:: ptr_rule_9.py | | | * Where ptr is pointing to simple variable, OR | | | | * Where ptr is pointing to any element in an array, OR | | | | * Where ptr is pointing to any element in a contiguous memory alloacted using malloc,realloc | | | | * Rule defined for int \*x, char \*x, float \*x, double \*x, struct ABC \*x, union ABC \*x | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 10 | **Pointers can be incremented or decremented relative to current position in a memory block** | .. literalinclude:: ptr_rule_10.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 11 | **Pointers can be indexed with positive or negative index relative to current position in a memory block** | .. literalinclude:: ptr_rule_11.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 12 | * **& and \* operators can be exchanged across LHS and RHS similar to an equation** | | | | | | | | * If \* moves from LHS to RHS it becomes \& | | | | * \*p = a; p = &a; | | | | | | | | * If \& moves from RHS to LHS it becomes \* | | | | * p = &a; \*p = a; | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 13 | **Subtraction of two pointers is allowed, when two pointers point to two blocks in contiguous memory** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 14 | **Addition of two pointers is NOT allowed** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 15 | **Multiplication of two pointers is NOT allowed** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 16 | **Division of two pointers is NOT allowed** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 17 | **& and \* on same side of the equation, cancel each other** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 18 | **\* on LHS and \* on RHS can cancel each other to get address** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 19 | **Single dimension simple array:** | .. literalinclude:: sd_int_arr.py | | | * Rule defined for int x[], char x[], float x[], double x[] | | | | * Element access will have at the most * or [ ] | | | | * Single pointer is used to store the address of single dimension array | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 20 | **Double dimension simple array:** | .. literalinclude:: dd_int_arr.py | | | * Rule defined for int x[][], char x[][], float x[][], double x[][] | | | | * Element access will have at the most ** or [ ][ ] or \*[ ] | | | | * Pointer to an array is used to store the address of double dimension array | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 21 | **Three dimension simple array:** | .. literalinclude:: 3d_int_arr.py | | | * Rule defined for int x[][], char x[][], float x[][], double x[][] | | | | * Element access will have at the most ** or [ ][ ] or \*[ ] | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 22 | **Simple Single pointer:** | | | | * Rule defined for int \*x, char \*x, float \*x, double \*x | | | | * Element access will have at the most * or [ ] | | | | * Single pointer can hold the address of a simple variable | | | | * Single pointer can hold the address of a simple single dimension array | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 23 | **Simple Double pointer:** | .. literalinclude:: dd_int_ptr.py | | | * Rule defined for int \*\*x, char \*\*x, float \*\*x, double \*\*x | | | | * Element access will have at the most ** or [ ][ ] or \*[ ] | | | | * **P** equals **&X** | | | | * **Q** equals **&P** | | | | * **\*P** equals **P[0]** equals **X** | | | | * **\*Q** equals **Q[0]** equals **P** | | | | * **\*Q** equals **Q[0]** equals **&X** | | | | * **\*\*Q** equals **Q[0][0]** equals **\*Q[0]** equals **X** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 24 | **Simple Triple pointer:** | .. literalinclude:: 3d_int_ptr.py | | | * Rule defined for int \*\*\*x, char \*\*\*x, float \*\*\*x, double\*\*\*x | | | | * Element access will have at the most \*\*\* or \*\*[] or \*[ ][ ] or [ ][ ][ ] | | | | * **P** equals **&X** | | | | * **Q** equals **&P** | | | | * **R** equals **&Q** | | | | * **\*P** equals **P[0]** equals **X** | | | | * **\*Q** equals **Q[0]** equals **P** | | | | * **\*Q** equals **Q[0]** equals **&X** | | | | * **\*\*Q** equals **Q[0][0]** equals **\*Q[0]** equals **X** | | | | * **\*R** equals **R[0]** equals **Q** | | | | * **\*R** equals **R[0]** equals **&P** | | | | * **\*\*R** equals **R[0][0]** equals **\*R[0]** equals **P** | | | | * **\*\*R** equals **R[0][0]** equals **\*R[0]** equals **&X** | | | | * **\*\*\*R** equals **R[0][0][0]** equals **\*\*R[0]** equals **\*R[0][0]** equals **X** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 25 | **Single dimension structure array:** | | | | * Element access will have at the most * or [ ] or -> | .. literalinclude:: struct_1.py | | | * Single pointer is used to store the address of single dimension array | | | | * LHS of **.(dot operator)** must evaluate to a structure instance | | | | * RHS of **.(dot operator)** must evaluate to a structure member | | | | * LHS of **->(structure dereference operator)** must evaluate to a structure instance | | | | * RHS of **->(structure dereference operator)** must evaluate to a structure member | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 26 | **Double dimension structure array:** | | | | * Element access will have at the most \*\* or [ ][ ] or \*-> or \*[ ] or [ ]-> | .. literalinclude:: struct_2.py | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 27 | **Three dimension structure array:** | .. literalinclude:: struct_3.py | | | * Element access will have at the most \*\*\* or [ ][ ][ ], OR | | | | * Element access will have at the most \*[ ][ ] or \*\*[ ], OR | | | | * Element access will have at the most \[ ][ ]-> or \*\*-> or \*[ ]-> | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 28 | **Structure Single pointer:** | .. literalinclude:: struct_ptr_1.py | | | * Rule defined for "struct ABC X; struct ABC \*Q;" | | | | * Element access will have at the most * or [ ] or -> | | | | * Single pointer can hold the address of a structure instance variable | | | | * Single pointer can hold the address of a single dimension struture array | | | | * LHS of **.(dot operator)** must evaluate to a structure instance | | | | * RHS of **.(dot operator)** must evaluate to a structure member | | | | * LHS of **->(structure dereference operator)** must evaluate to a structure instance | | | | * RHS of **->(structure dereference operator)** must evaluate to a structure member | | | | * **P** equals **&X** | | | | * **(\*P)\.** equals **P->** equals **P[0]** equals **X** | | | | * **(\*Q)\.** equals **Q->** equals **Q[ ]** equals **A[]** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 29 | **Structure Double pointer:** | .. literalinclude:: struct_ptr_2.py | | | * Rule defined for "struct ABC \*\*Q;" | | | | * Element access will have at the most \*\* or [ ][ ] or \*-> or \*[ ] or [ ]-> | | | | * LHS of **.(dot operator)** must evaluate to a structure instance | | | | * RHS of **.(dot operator)** must evaluate to a structure member | | | | * LHS of **->(structure dereference operator)** must evaluate to a structure instance | | | | * RHS of **->(structure dereference operator)** must evaluate to a structure member | | | | * **P** equals **&X** | | | | * **Q** equals **&P** | | | | * **(\*P)\.** equals **P->** equals **P[0]** equals **X** | | | | * **\*Q** equals **Q[0]** equals **P** | | | | * **\*Q** equals **Q[0]** equals **&X** | | | | * **(\*(\*Q)).** equals **(\*(Q[0])).** equals **Q[0][0].** equals **X** | | | | * **Q[0]->** equals **(\*Q)->** equals **X** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+ | Rule 30 | **Structure Triple pointer:** | .. literalinclude:: struct_ptr_3.py | | | * Rule defined for "struct ABC \*\*\*Q;" | | | | * Element access will have at the most \*\*\* or [ ][ ][ ], OR | | | | * Element access will have at the most \*[ ][ ] or \*\*[ ], OR | | | | * Element access will have at the most \[ ][ ]-> or \*\*-> or \*[ ]-> | | | | * LHS of **.(dot operator)** must evaluate to a structure instance | | | | * RHS of **.(dot operator)** must evaluate to a structure member | | | | * LHS of **->(structure dereference operator)** must evaluate to a structure instance | | | | * RHS of **->(structure dereference operator)** must evaluate to a structure member | | | | * **P** equals **&X** | | | | * **Q** equals **&P** | | | | * **R** equals **&Q** | | | | * **(\*P)\.** equals **P->** equals **P[0]** equals **X** | | | | * **\*Q** equals **Q[0]** equals **P** | | | | * **\*\*Q** equals **Q[0][0]** equals **\*Q[0]** equals **X** | | | | * **\*R** equals **R[0]** equals **Q** | | | | * **\*R** equals **R[0]** equals **&P** | | | | * **\*\*R** equals **R[0][0]** equals **\*R[0]** equals **P** | | | | * **\*\*R** equals **R[0][0]** equals **\*R[0]** equals **&X** | | | | * **(*(*(*R))).** equals **(*(*R))->** equals **(*(*R))[0].** equals **(*R)[0][0].** equals **X** | | | | * **(*R)[0]->** equals **R[0][0][0].** equals **R[0][0]->** equals **(*(R[0][0])).** equals **X** | | +----------+------------------------------------------------------------------------------------------------------------+-------------------------------------+