post decrement struct 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 structure array 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)--`` ? .. 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)--`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is the difference between ``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)--`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics of Post Decrement * :ref:`Basics of Post Decrement <post_decr_struct_sp_mt_1>` * :ref:`Meaning of expressions <post_decr_struct_sp_mt_2>` * :ref:`Summary of expressions <post_decr_struct_sp_mt_3>` * :ref:`Post Decrement : struct pointer inside structure <post_decr_struct_sp_mt_4>` * :ref:`Post Decrement : Function Call <post_decr_struct_sp_mt_5>` .. _post_decr_struct_sp_mt_1: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Basics of Post Decrement * :ref:`ptr-- Basic Usage <post_decr_struct_sp_ex_1>` * :ref:`ptr-- : Iterate and print structures of an structure array <post_decr_struct_sp_ex_2>` .. _post_decr_struct_sp_ex_1: .. tab-set:: .. tab-item:: Example 1 : ``ptr--`` : Basic Usage * Step 1 : Define a Single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Point ``ptr`` to Single dimension array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 3 : Decrement ``ptr`` .. code-block:: c ptr--; * Step 4 : Print ``*ptr`` .. code-block:: c printf("ptr->a = %d\n", ptr->a); printf("ptr->b = %d\n", ptr->b); * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; ptr--; printf("ptr->a = %d\n", ptr->a); printf("ptr->b = %d\n", ptr->b); return 0; } * Output is as below .. code-block:: c ptr->a = 2 ptr->b = 3 .. _post_decr_struct_sp_ex_2: .. tab-set:: .. tab-item:: Example 2 : ``ptr--`` : Iterate and print structures of an structure array * Step 1 : Define a Single Dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Point ``ptr`` to Single dimension array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 3 : Print structure array by iterating through all structures using ``ptr--`` .. code-block:: c for (int i = 0; i < 3; i++) { printf("ptr->a = %d\n", ptr->a); printf("ptr->b = %d\n", ptr->b); ptr--; } * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { printf("ptr->a = %d\n", ptr->a); printf("ptr->b = %d\n", ptr->b); ptr--; } return 0; } * Output is as below .. code-block:: c ptr->a = 4 ptr->b = 5 ptr->a = 2 ptr->b = 3 ptr->a = 0 ptr->b = 1 .. _post_decr_struct_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-- <post_decr_struct_sp_ex_6>` * :ref:`Meaning of *(ptr--) and c = *(ptr--) <post_decr_struct_sp_ex_7>` * :ref:`Meaning of *ptr-- and c = *ptr-- <post_decr_struct_sp_ex_10>` * :ref:`Meaning of *(ptr)-- and c = *(ptr)-- <post_decr_struct_sp_ex_8>` * :ref:`Meaning of (*ptr)-- and c = (*ptr)-- <post_decr_struct_sp_ex_9>` .. _post_decr_struct_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 * Current value of ``ptr`` is assigned to ``p`` * ``ptr`` is decremented * We now derived a rule * First Assign, then decrement * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Define a single pointer and point to array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; OR .. code-block:: c struct User_Data *ptr; ptr = &arr[0]; * Step 3 : Iterate and print the structure array .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data *p; p = ptr--; printf("p->a = %d\n", p->a); printf("p->b = %d\n", p->b); } Output is as below .. code-block:: 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 p = ptr--; * There are two steps in this statement * Current value of ``ptr`` is assigned to ``p`` * ``ptr`` is decremented * We now derived a rule * First Assign, then decrement * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data *p; p = ptr--; printf("p->a = %d\n", p->a); printf("p->b = %d\n", p->b); } return 0; } * Output is as below .. code-block:: c p->a = 4 p->b = 5 p->a = 2 p->b = 3 p->a = 0 p->b = 1 .. _post_decr_struct_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 * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is decremented * ``*ptr`` is NOT decremented * We now derived a rule * First Assign, then decrement * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Define a single pointer and point to array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; OR .. code-block:: c struct User_Data *ptr; ptr = &arr[0]; * Step 3 : Iterate and print the structure array .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *(ptr--); printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } Output is as below .. code-block:: 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 * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is decremented * ``*ptr`` is NOT decremented * We now derived a rule * First Assign, then decrement * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *(ptr--); printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_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--) <post_decr_struct_sp_ex_7>` .. 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 * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is decremented * ``*ptr`` is NOT decremented * We now derived a rule * First Assign, then decrement * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Define a single pointer and point to array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; OR .. code-block:: c struct User_Data *ptr; ptr = &arr[0]; * Step 3 : Iterate and print the structure array .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } Output is as below .. code-block:: 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 * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is decremented * ``*ptr`` is NOT decremented * We now derived a rule * First Assign, then decrement * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_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--) <post_decr_struct_sp_ex_7>` .. 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 * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is decremented * ``*ptr`` is NOT decremented * We now derived a rule * First Assign, then decrement * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Define a single pointer and point to array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; OR .. code-block:: c struct User_Data *ptr; ptr = &arr[0]; * Step 3 : Iterate and print the structure array .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *(ptr)--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } Output is as below .. code-block:: 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 * Current value of ``*ptr`` is assigned to ``c`` * ``ptr`` is decremented * ``*ptr`` is NOT decremented * We now derived a rule * First Assign, then decrement * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *(ptr)--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_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)--; * This is invalid usage for a structure pointer ! .. _post_decr_struct_sp_mt_3: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Summary of expressions * :ref:`Summary of Post Decrement expressions <post_decr_struct_sp_ex_summary>` .. _post_decr_struct_sp_ex_summary: .. tab-set:: .. tab-item:: Summary of Post Decrement expressions ===================================== ========================================================================= Expression Explanation ===================================== ========================================================================= c = ptr-- * Assign ``ptr`` to ``c`` * Decrement ``ptr`` c = \*(ptr--) * Assign ``*ptr`` to ``c`` * Decrement ``ptr`` * DO NOT decrement ``*ptr`` c = \*ptr-- * Assign ``*ptr`` to ``c`` * Decrement ``ptr`` * DO NOT decrement ``*ptr`` c = \*(ptr)-- * Assign ``*ptr`` to ``c`` * Decrement ``ptr`` * DO NOT decrement ``*ptr`` c = (\*ptr)-- * INVALID for structure pointer ===================================== ========================================================================= .. _post_decr_struct_sp_mt_4: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Post Decrement : struct pointer inside structure * :ref:`Structure pointer accessed using Structure Object <post_decr_struct_sp_ex_struct_ex_1>` * :ref:`Structure pointer accessed using Structure Single Pointer <post_decr_struct_sp_ex_struct_ex_2>` * :ref:`Structure pointer accessed using Structure Double Pointer <post_decr_struct_sp_ex_struct_ex_3>` * :ref:`Structure pointer accessed using Structure Triple Pointer <post_decr_struct_sp_ex_struct_ex_4>` * :ref:`Structure pointer accessed using Nested Structure Object <post_decr_struct_sp_ex_struct_ex_5>` * :ref:`Structure pointer accessed using Nested Structure Single Pointer <post_decr_struct_sp_ex_struct_ex_6>` * :ref:`Structure pointer accessed using Nested Structure 2 level Pointers <post_decr_struct_sp_ex_struct_ex_7>` .. _post_decr_struct_sp_ex_struct_ex_1: .. tab-set:: .. tab-item:: Structure pointer accessed using Structure Object * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Create a structure object .. code-block:: c struct ABC { struct User_Data *ptr; }; struct ABC abc; * Step 3 : Point single structure pointer to single dimension array .. code-block:: c abc.ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *abc.ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; struct ABC { struct User_Data *ptr; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct ABC abc; abc.ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *abc.ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_ex_struct_ex_2: .. tab-set:: .. tab-item:: Structure pointer accessed using Structure Single Pointer * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC { struct User_Data *ptr; }; struct ABC *sp; sp = malloc(sizeof(struct ABC)); * Step 3 : Point single structure pointer to single dimension array .. code-block:: c sp->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *sp->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } * Step 5 : Free memory after use .. code-block:: c free(sp); * See full program below .. code-block:: c #include <stdio.h> #include <stdlib.h> struct User_Data { int a; int b; }; struct ABC { struct User_Data *ptr; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct ABC *sp; sp = malloc(sizeof(struct ABC)); sp->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *sp->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } free(sp); return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_ex_struct_ex_3: .. tab-set:: .. tab-item:: Structure pointer accessed using Structure Double Pointer * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC { struct User_Data *ptr; }; struct ABC **dp; dp = malloc(sizeof(struct ABC *)); *dp = malloc(sizeof(struct ABC )); * Step 3 : Point single structure pointer to single dimension array .. code-block:: c (*dp)->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *(*dp)->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } * Step 5 : Free memory after use .. code-block:: c free(*dp); free(dp); * See full program below .. code-block:: c #include <stdio.h> #include <stdlib.h> struct User_Data { int a; int b; }; struct ABC { struct User_Data *ptr; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct ABC **dp; dp = malloc(sizeof(struct ABC *)); *dp = malloc(sizeof(struct ABC )); (*dp)->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *(*dp)->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } free(*dp); free(dp); return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_ex_struct_ex_4: .. tab-set:: .. tab-item:: Structure pointer accessed using Structure Triple Pointer * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct ABC { struct User_Data *ptr; }; struct ABC ***dp; dp = malloc(sizeof(struct ABC **)); *dp = malloc(sizeof(struct ABC *)); **dp = malloc(sizeof(struct ABC )); * Step 3 : Point single structure pointer to single dimension array .. code-block:: c (**dp)->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *(**dp)->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } * Step 5 : Free memory after use .. code-block:: c free(**dp); free(*dp); free(dp); * See full program below .. code-block:: c #include <stdio.h> #include <stdlib.h> struct User_Data { int a; int b; }; struct ABC { struct User_Data *ptr; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct ABC ***dp; dp = malloc(sizeof(struct ABC **)); *dp = malloc(sizeof(struct ABC *)); **dp = malloc(sizeof(struct ABC )); (**dp)->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *(**dp)->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } free(**dp); free(*dp); free(dp); return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_ex_struct_ex_5: .. tab-set:: .. tab-item:: Structure pointer accessed using Nested Structure Object * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Create a structure object .. code-block:: c struct PQR { struct User_Data *ptr; }; struct ABC { struct PQR pqr; }; struct ABC abc; * Step 3 : Point single structure pointer to single dimension array .. code-block:: c abc.pqr.ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *abc.pqr.ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; struct PQR { struct User_Data *ptr; }; struct ABC { struct PQR pqr; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct ABC abc; abc.pqr.ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *abc.pqr.ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_ex_struct_ex_6: .. tab-set:: .. tab-item:: Structure pointer accessed using Nested Structure Single Pointer * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct PQR { struct User_Data *ptr; }; struct ABC { struct PQR pqr; }; struct ABC *sp; sp = malloc(sizeof(struct ABC)); * Step 3 : Point single structure pointer to single dimension array .. code-block:: c sp->pqr.ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *sp->pqr.ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } * Step 5 : Free memory after use .. code-block:: c free(sp); * See full program below .. code-block:: c #include <stdio.h> #include <stdlib.h> struct User_Data { int a; int b; }; struct PQR { struct User_Data *ptr; }; struct ABC { struct PQR pqr; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct ABC *sp; sp = malloc(sizeof(struct ABC)); sp->pqr.ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *sp->pqr.ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } free(sp); return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_ex_struct_ex_7: .. tab-set:: .. tab-item:: Structure pointer accessed using Nested Structure 2 level Pointers * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Allocate memory for structure pointers .. code-block:: c struct PQR { struct User_Data *ptr; }; struct ABC { struct PQR *pqr; }; struct ABC *abc; abc = malloc(sizeof(struct ABC)); abc->pqr = malloc(sizeof(struct PQR)); * Step 3 : Point single structure pointer to single dimension array .. code-block:: c abc->pqr->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 4 : Iterate through single dimension array using pointer .. code-block:: c for (int i = 0; i < 3; i++) { struct User_Data c; c = *abc->pqr->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } * Step 5 : Free memory after use .. code-block:: c free(abc->pqr); free(abc); * See full program below .. code-block:: c #include <stdio.h> #include <stdlib.h> struct User_Data { int a; int b; }; struct PQR { struct User_Data *ptr; }; struct ABC { struct PQR *pqr; }; int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct ABC *abc; abc = malloc(sizeof(struct ABC)); abc->pqr = malloc(sizeof(struct PQR)); abc->pqr->ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { struct User_Data c; c = *abc->pqr->ptr--; printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } free(abc->pqr); free(abc); return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_mt_5: .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Post Decrement : Function Call * :ref:`*ptr-- : Function Call <post_decr_struct_sp_call_by_value_ex_1>` * :ref:`ptr-- : Function Call <post_decr_struct_sp_call_by_value_ex_2>` .. _post_decr_struct_sp_call_by_value_ex_1: .. tab-set:: .. tab-item:: \*ptr-- : Function Call * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Define a single pointer and point to array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 3 : Iterate array using ptr-- and Pass by Value .. code-block:: c for (int i = 0; i < 3; i++) { fun(*ptr--); } * Step 4 : Define a function ``fun`` which receives a structure from caller .. code-block:: c void fun(struct User_Data c) { printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us Recall, * In case of ``*ptr--`` * Current value of ``*ptr`` is first assigned, then ``ptr`` decremented * Hence, in this case * Current value of ``*ptr`` is first passed to function ``fun``, then ``ptr`` decremented * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; void fun(struct User_Data c) { printf("c.a = %d\n", c.a); printf("c.b = %d\n", c.b); } int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { fun(*ptr--); } return 0; } * Output is as below .. code-block:: c c.a = 4 c.b = 5 c.a = 2 c.b = 3 c.a = 0 c.b = 1 .. _post_decr_struct_sp_call_by_value_ex_2: .. tab-set:: .. tab-item:: ptr-- : Function Call * Step 1 : Define a single dimension array .. code-block:: c struct User_Data { int a; int b; }; struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; * Step 2 : Define a single pointer and point to array .. code-block:: c struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; * Step 3 : Iterate array using ptr-- and Pass by Value .. code-block:: c for (int i = 0; i < 3; i++) { fun(ptr--); } * Step 4 : Define a function ``fun`` which receives a structure pointer from caller .. code-block:: c void fun(struct User_Data *c) { printf("c->a = %d\n", c->a); printf("c->b = %d\n", c->b); } .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Let us Recall, * In case of ``ptr--`` * Current value of ``ptr`` is first assigned, then decremented * Hence, in this case * Current value of ``ptr`` is first passed to function ``fun``, then decremented * See full program below .. code-block:: c #include <stdio.h> struct User_Data { int a; int b; }; void fun(struct User_Data *c) { printf("c->a = %d\n", c->a); printf("c->b = %d\n", c->b); } int main(void) { struct User_Data arr[3] = { { .a = 0, .b = 1 }, { .a = 2, .b = 3 }, { .a = 4, .b = 5 }, }; struct User_Data *ptr; ptr = arr + sizeof(arr)/sizeof(arr[0]) - 1; for (int i = 0; i < 3; i++) { fun(ptr--); } return 0; } * Output is as below .. code-block:: c c->a = 4 c->b = 5 c->a = 2 c->b = 3 c->a = 0 c->b = 1 .. card:: See Also * Current Module * :doc:`../post_decr_ptr` * Previous Module * :doc:`../../pre_decr_ptr/pre_decr_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:`../../function_ptr/function_ptr` * :doc:`../../ptr_to_array/ptr_to_array` * :doc:`../../pre_incr_ptr/pre_incr_ptr` * :doc:`../../post_incr_ptr/post_incr_ptr`