pre increment int single pointer ==================================== In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow How to iterate integer arrays using ++ptr ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the meaning of ``++ptr``, ``*++ptr``, ``*(++ptr)``, ``*++(ptr)``, ``++(*ptr)``, ``++*ptr`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the meaning of ``c = ++ptr``, ``c = *++ptr``, ``c = *(++ptr)``, ``c = *++(ptr)``, ``c = ++(*ptr)``, ``c = ++*ptr`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the difference between ``++ptr``, ``*++ptr``, ``*(++ptr)``, ``*++(ptr)``, ``++(*ptr)``, ``++*ptr`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the difference between ``c = ++ptr``, ``c = *++ptr``, ``c = *(++ptr)``, ``c = *++(ptr)``, ``c = ++(*ptr)``, ``c = ++*ptr`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics of Pre Increment * :ref:`Basics of Pre Increment ` * :ref:`Meaning of expressions ` * :ref:`Summary of expressions ` * :ref:`Pre Increment : int pointer inside structure ` * :ref:`Pre Increment : Function Call ` .. _pre_incr_int_sp_mt_1: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Basics of Pre Increment * :ref:`++ptr Basic Usage ` * :ref:`++ptr : Iterate and print integers of an integer : Method 1 ` * :ref:`++ptr : Iterate and print integers of an integer : Method 2 ` * :ref:`++ptr : Iterate and print integers of an integer : Method 3 ` * :ref:`++ptr : Iterate and print integers of an integer : Method 4 ` .. _pre_incr_int_sp_ex_1: .. tab-set:: .. tab-item:: Example 1 : ``++ptr`` : Basic Usage * Step 1 : Define a Single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Point ``ptr`` to Single dimension array .. code-block:: c int *ptr; ptr = a; * Step 3 : Increment ``ptr`` .. code-block:: c ++ptr; * Step 4 : Print ``*ptr`` .. code-block:: c printf("*ptr = %d\n", *ptr); * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; ++ptr; printf("*ptr = %d\n", *ptr); return 0; } * Output is as below .. code-block:: c *ptr = 1 .. _pre_incr_int_sp_ex_2: .. tab-set:: .. tab-item:: Example 2 : ``++ptr`` : Iterate and print integers of an integer : Method 1 * Step 1 : Define a Single Dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Point ``ptr`` to Single dimension array .. code-block:: c int *ptr; ptr = a; * Step 3 : Print the integer array by iterating through all integers using ``++ptr`` .. code-block:: c for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) { printf("*ptr = %d\n", *ptr); ++ptr; } * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) { printf("*ptr = %d\n", *ptr); ++ptr; } return 0; } * Output is as below .. code-block:: c *ptr = 0 *ptr = 1 *ptr = 2 *ptr = 3 *ptr = 4 *ptr = 5 *ptr = 6 .. _pre_incr_int_sp_ex_3: .. tab-set:: .. tab-item:: Example 3 : ``++ptr`` : Iterate and print integers of an integer : Method 2 * Step 1 : Define a Single Dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Point ``ptr`` to Single dimension array .. code-block:: c int *ptr; ptr = a; * Step 3 : Print the integer array by iterating through all integers using ``++ptr`` .. code-block:: c while (ptr <= (a + sizeof(a)/sizeof(a[0])) - 1) { printf("*ptr = %d\n", *ptr); ++ptr; } * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr <= (a + sizeof(a)/sizeof(a[0])) - 1) { printf("*ptr = %d\n", *ptr); ++ptr; } return 0; } * Output is as below .. code-block:: c *ptr = 0 *ptr = 1 *ptr = 2 *ptr = 3 *ptr = 4 *ptr = 5 *ptr = 6 .. _pre_incr_int_sp_ex_4: .. tab-set:: .. tab-item:: Example 4 : ``++ptr`` : Iterate and print integers of an integer : Method 3 * Step 1 : Define a Single Dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Point ``ptr`` to Single dimension array .. code-block:: c int *ptr; ptr = a; * Step 3 : Print the integer array by iterating through all integers using ``++ptr`` .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++ptr; printf("*ptr = %d\n", c); } * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++ptr; printf("*ptr = %d\n", c); } return 0; } * Output is as below .. code-block:: c *ptr = 1 *ptr = 2 *ptr = 3 *ptr = 4 *ptr = 5 *ptr = 6 .. dropdown:: Guess what is going on ? * Statement ``c = *++ptr`` can be understood in two steps * Step 1 : Increment ``ptr`` * Step 2 : Assign ``*ptr`` to ``c`` .. _pre_incr_int_sp_ex_5: .. tab-set:: .. tab-item:: Example 5 : ``++ptr`` : Iterate and print integers of an integer : Method 4 * Step 1 : Define a Single Dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Point ``ptr`` to Single dimension array .. code-block:: c int *ptr; ptr = a; * Step 3 : Print the integer array by iterating through all integers using ``++ptr`` .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { printf("*ptr = %d\n", *++ptr); } * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { printf("*ptr = %d\n", *++ptr); } return 0; } * Output is as below .. code-block:: c *ptr = 1 *ptr = 2 *ptr = 3 *ptr = 4 *ptr = 5 *ptr = 6 .. dropdown:: Guess what is going on ? * Statement ``printf("*ptr = %d\n", *++ptr);`` can be understood in two steps * Step 1 : Increment ``ptr`` * Step 2 : Pass ``*ptr`` as arguement to ``printf`` .. _pre_incr_int_sp_mt_2: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Meaning of expressions * :ref:`Meaning of ++ptr and c = ++ptr ` * :ref:`Meaning of *++ptr and c = *++ptr ` * :ref:`Meaning of *(++ptr) and c = *(++ptr) ` * :ref:`Meaning of *++(ptr) and c = *++(ptr) ` * :ref:`Meaning of ++*ptr and c = ++*ptr ` * :ref:`Meaning of ++(*ptr) and c = ++(*ptr) ` .. _pre_incr_int_sp_ex_6: .. tab-set:: .. tab-item:: Meaning of ``++ptr`` and ``c = ++ptr`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Consider statement .. code-block:: c p = ++ptr; * There are two steps in this statement * ``ptr`` is incremented * Current value of ``ptr`` is assigned to ``p`` * We now derived a rule * First Increment, then Assign * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Define a single pointer and point to array .. code-block:: c int *ptr; ptr = a; OR .. code-block:: c int *ptr; ptr = &a[0]; * Step 3 : Iterate and print the integer array .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int *p; p = ++ptr; printf("*p = %d\n", *p); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Let us Recall .. code-block:: c p = ++ptr; * There are two steps in this statement * ``ptr`` is incremented * Current value of ``ptr`` is assigned to ``p`` * We now derived a rule * First Increment, then Assign * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int *p; p = ++ptr; printf("*p = %d\n", *p); } return 0; } Output is as below .. code-block:: c *p = 1 *p = 2 *p = 3 *p = 4 *p = 5 *p = 6 .. _pre_incr_int_sp_ex_7: .. tab-set:: .. tab-item:: Meaning of ``*(++ptr)`` and ``c = *(++ptr)`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Consider statement .. code-block:: c c = *(++ptr); * There are two steps in this statement * ``ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``*ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Define a single pointer and point to array .. code-block:: c int *ptr; ptr = a; OR .. code-block:: c int *ptr; ptr = &a[0]; * Step 3 : Iterate and print the integer array .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *(++ptr); printf("c = %d\n", c); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Let us Recall .. code-block:: c c = *(++ptr); * There are two steps in this statement * ``ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``*ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *(++ptr); printf("c = %d\n", c); } return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_10: .. tab-set:: .. tab-item:: Meaning of ``*++ptr`` and c = ``*++ptr`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * :ref:`Same as *(++ptr) and c = *(++ptr) ` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Consider statement .. code-block:: c c = *++ptr; * There are two steps in this statement * ``ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``*ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Define a single pointer and point to array .. code-block:: c int *ptr; ptr = a; OR .. code-block:: c int *ptr; ptr = &a[0]; * Step 3 : Iterate and print the integer array .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++ptr; printf("c = %d\n", c); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Let us Recall .. code-block:: c c = *++ptr; * There are two steps in this statement * ``ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``*ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++ptr; printf("c = %d\n", c); } return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_8: .. tab-set:: .. tab-item:: Meaning of ``*++(ptr)`` and ``c = *++(ptr)`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * :ref:`Same as *(++ptr) and c = *(++ptr) ` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Consider statement .. code-block:: c c = *++(ptr); * There are two steps in this statement * ``ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``*ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Define a single pointer and point to array .. code-block:: c int *ptr; ptr = a; OR .. code-block:: c int *ptr; ptr = &a[0]; * Step 3 : Iterate and print the integer array .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++(ptr); printf("c = %d\n", c); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Can you guess what is happening ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Let us Recall .. code-block:: c c = *++(ptr); * There are two steps in this statement * ``ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``*ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign * See full program below .. code-block:: c #include int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++(ptr); printf("c = %d\n", c); } return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_9: .. tab-set:: .. tab-item:: Meaning of ``++*ptr`` and ``c = ++*ptr`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Consider statement .. code-block:: c c = ++*ptr; * There are two steps in this statement * ``*ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign .. _pre_incr_int_sp_ex_11: .. tab-set:: .. tab-item:: Meaning of ``++(*ptr)`` and ``c = ++(*ptr)`` .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * Consider statement .. code-block:: c c = ++(*ptr); * There are two steps in this statement * ``*ptr`` is incremented * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is NOT incremented * We now derived a rule * First Increment, then Assign .. _pre_incr_int_sp_mt_3: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Summary of expressions * :ref:`Summary of Pre Increment expressions ` .. _pre_incr_int_sp_ex_summary: .. tab-set:: .. tab-item:: Summary of Pre Increment expressions ===================================== ========================================================================= Expression Explanation ===================================== ========================================================================= c = ++ptr * Increment ``ptr`` * Assign ``ptr`` to ``c`` c = \*++ptr * Increment ``ptr`` * Assign ``*ptr`` to ``c`` * DO NOT increment ``*ptr`` c = \*(++ptr) * Increment ``ptr`` * Assign ``*ptr`` to ``c`` * DO NOT increment ``*ptr`` c = \*++(ptr) * Increment ``ptr`` * Assign ``*ptr`` to ``c`` * DO NOT increment ``*ptr`` c = \++*ptr * Increment ``*ptr`` * Assign ``*ptr`` to ``c`` * DO NOT increment ``ptr`` c = \++*(ptr) * Increment ``*ptr`` * Assign ``*ptr`` to ``c`` * DO NOT increment ``ptr`` ===================================== ========================================================================= .. _pre_incr_int_sp_mt_4: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Pre Increment : int pointer inside structure * :ref:`Integer pointer accessed using Structure Object ` * :ref:`Integer pointer accessed using Structure Single Pointer ` * :ref:`Integer pointer accessed using Structure Double Pointer ` * :ref:`Integer pointer accessed using Structure Triple Pointer ` * :ref:`Integer pointer accessed using Nested Structure Object ` * :ref:`Integer pointer accessed using Nested Structure Single Pointer ` * :ref:`Integer pointer accessed using Nested Structure 2 level Pointers ` .. _pre_incr_int_sp_ex_struct_ex_1: .. tab-set:: .. tab-item:: Integer pointer accessed using Structure Object * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Create a structure object .. code-block:: c struct ABC abc; * Step 3 : Point single integer pointer to single dimension array .. code-block:: c abc.ptr = a; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c while (abc.ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++abc.ptr; printf("c = %d\n", c); } * See full program below .. code-block:: c #include struct ABC { int *ptr; }; int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; struct ABC abc; abc.ptr = a; while (abc.ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++abc.ptr; printf("c = %d\n", c); } return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_struct_ex_2: .. tab-set:: .. tab-item:: Integer pointer accessed using Structure Single Pointer * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC *sp; sp = malloc(sizeof(struct ABC)); * Step 3 : Point single integer pointer to single dimension array .. code-block:: c sp->ptr = a; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c while (sp->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++sp->ptr; printf("c = %d\n", c); } * Step 5 : Free memory after use .. code-block:: c free(sp); * See full program below .. code-block:: c #include #include struct ABC { int *ptr; }; int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; struct ABC *sp; sp = malloc(sizeof(struct ABC)); sp->ptr = a; while (sp->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++sp->ptr; printf("c = %d\n", c); } free(sp); return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_struct_ex_3: .. tab-set:: .. tab-item:: Integer pointer accessed using Structure Double Pointer * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC **dp; dp = malloc(sizeof(struct ABC *)); *dp = malloc(sizeof(struct ABC )); * Step 3 : Point single integer pointer to single dimension array .. code-block:: c (*dp)->ptr = a; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c while ((*dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++(*dp)->ptr; printf("c = %d\n", c); } * Step 5 : Free memory after use .. code-block:: c free(*dp); free(dp); * See full program below .. code-block:: c #include #include struct ABC { int *ptr; }; int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; struct ABC **dp; dp = malloc(sizeof(struct ABC *)); *dp = malloc(sizeof(struct ABC )); (*dp)->ptr = a; while ((*dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++(*dp)->ptr; printf("c = %d\n", c); } free(*dp); free(dp); return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_struct_ex_4: .. tab-set:: .. tab-item:: Integer pointer accessed using Structure Triple Pointer * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC ***dp; dp = malloc(sizeof(struct ABC **)); *dp = malloc(sizeof(struct ABC *)); **dp = malloc(sizeof(struct ABC )); * Step 3 : Point single integer pointer to single dimension array .. code-block:: c (**dp)->ptr = a; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c while ((**dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++(**dp)->ptr; printf("c = %d\n", c); } * Step 5 : Free memory after use .. code-block:: c free(**dp); free(*dp); free(dp); * See full program below .. code-block:: c #include #include struct ABC { int *ptr; }; int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; struct ABC ***dp; dp = malloc(sizeof(struct ABC **)); *dp = malloc(sizeof(struct ABC *)); **dp = malloc(sizeof(struct ABC )); (**dp)->ptr = a; while ((**dp)->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++(**dp)->ptr; printf("c = %d\n", c); } free(**dp); free(*dp); free(dp); return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_struct_ex_5: .. tab-set:: .. tab-item:: Integer pointer accessed using Nested Structure Object * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Create a structure object .. code-block:: c struct ABC abc; * Step 3 : Point single integer pointer to single dimension array .. code-block:: c abc.pqr.ptr = a; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c while (abc.pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++abc.pqr.ptr; printf("c = %d\n", c); } * See full program below .. code-block:: c #include struct PQR { int *ptr; }; struct ABC { struct PQR pqr; }; int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; struct ABC abc; abc.pqr.ptr = a; while (abc.pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++abc.pqr.ptr; printf("c = %d\n", c); } return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_struct_ex_6: .. tab-set:: .. tab-item:: Integer pointer accessed using Nested Structure Single Pointer * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC *sp; sp = malloc(sizeof(struct ABC)); * Step 3 : Point single integer pointer to single dimension array .. code-block:: c sp->pqr.ptr = a; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c while (sp->pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++sp->pqr.ptr; printf("c = %d\n", c); } * Step 5 : Free memory after use .. code-block:: c free(sp); * See full program below .. code-block:: c #include #include struct PQR { int *ptr; }; struct ABC { struct PQR pqr; }; int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; struct ABC *sp; sp = malloc(sizeof(struct ABC)); sp->pqr.ptr = a; while (sp->pqr.ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++sp->pqr.ptr; printf("c = %d\n", c); } free(sp); return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_ex_struct_ex_7: .. tab-set:: .. tab-item:: Integer pointer accessed using Nested Structure 2 level Pointers * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC *abc; abc = malloc(sizeof(struct ABC)); abc->pqr = malloc(sizeof(struct PQR)); * Step 3 : Point single integer pointer to single dimension array .. code-block:: c abc->pqr->ptr = a; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c while (abc->pqr->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++abc->pqr->ptr; printf("c = %d\n", c); } * Step 5 : Free memory after use .. code-block:: c free(abc->pqr); free(abc); * See full program below .. code-block:: c #include #include struct PQR { int *ptr; }; struct ABC { struct PQR *pqr; }; int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; struct ABC *abc; abc = malloc(sizeof(struct ABC)); abc->pqr = malloc(sizeof(struct PQR)); abc->pqr->ptr = a; while (abc->pqr->ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { int c; c = *++abc->pqr->ptr; printf("c = %d\n", c); } free(abc->pqr); free(abc); return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_mt_5: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Pre Increment : Function Call * :ref:`*++ptr : Function Call ` * :ref:`++ptr : Function Call ` .. _pre_incr_int_sp_call_by_value_ex_1: .. tab-set:: .. tab-item:: \*++ptr : Function Call * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Define a single pointer and point to array .. code-block:: c int *ptr; ptr = a; * Step 3 : Iterate array using ++ptr and Pass by Value .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { fun(*++ptr); } * Step 4 : Define a function ``fun`` which receives a integer from caller .. code-block:: c void fun(int c) { printf("c = %d\n", c); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us Recall, * In case of ``*++ptr`` * ``ptr`` is incremented first, then current value of ``*ptr`` is assigned * Hence, in this case * ``ptr`` is incremented first, then Current value of ``*ptr`` is passed to function ``fun`` * See full program below .. code-block:: c #include void fun(int c) { printf("c = %d\n", c); } int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { fun(*++ptr); } return 0; } Output is as below .. code-block:: c c = 1 c = 2 c = 3 c = 4 c = 5 c = 6 .. _pre_incr_int_sp_call_by_value_ex_2: .. tab-set:: .. tab-item:: ++ptr : Function Call * Step 1 : Define a single dimension array .. code-block:: c int a[7] = {0, 1, 2, 3, 4, 5, 6}; * Step 2 : Define a single pointer and point to array .. code-block:: c int *ptr; ptr = a; * Step 3 : Iterate array using ++ptr and Pass by Value .. code-block:: c while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { fun(++ptr); } * Step 4 : Define a function ``fun`` which receives a integer pointer from caller .. code-block:: c void fun(int *c) { printf("*c = %d\n", *c); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us Recall, * In case of ``++ptr`` * ``ptr`` is incremented first, then incremented * Hence, in this case * ``ptr`` is incremented first, then passed to function ``fun`` * See full program below .. code-block:: c #include void fun(int *c) { printf("*c = %d\n", *c); } int main(void) { int a[7] = {0, 1, 2, 3, 4, 5, 6}; int *ptr; ptr = a; while (ptr < (a + sizeof(a)/sizeof(a[0])) - 1) { fun(++ptr); } return 0; } Output is as below .. code-block:: c *c = 1 *c = 2 *c = 3 *c = 4 *c = 5 *c = 6 .. card:: See Also * Current Module * :doc:`../pre_incr_ptr` * Previous Module * :doc:`../../function_ptr/function_ptr` * Next Module * :doc:`../../post_incr_ptr/post_incr_ptr` * Other Modules * :doc:`../../variable_and_ptr/variable_and_ptr` * :doc:`../../array_n_ptrs/array_n_ptrs` * :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:`../../pre_decr_ptr/pre_decr_ptr` * :doc:`../../post_decr_ptr/post_decr_ptr`