const struct single pointer =========================== In this section, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``const struct ABC`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``struct ABC const`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``const struct ABC *p = &a;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``struct ABC const *p = &a;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``struct ABC *const p = &a;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``const struct ABC *const p = &a;`` ? * Inorder to answer above questions, let us remember a very simple rule .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Anything after ``const`` keyword CAN NOT be changed * Is it that simple ? * Yes. Let us see how do we apply above rule to answer the questions .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Example 1 : const struct ABC a; ` * :ref:`Example 2 : const struct ABC *p = &a; ` * :ref:`Example 3 : struct ABC *const p = &a ;` * :ref:`Example 4 : const struct ABC *const p = &a; ` * :ref:`Summary ` .. _const_ptr_struct_sp_ex_1: .. tab-set:: .. tab-item:: Example 1 : const struct ABC a * Step 1 : Consider the statement .. code-block:: c struct ABC { int x; int y; }; const struct ABC a = { .x = 10, .y = 20 }; * Step 2 : Remove all keywords after ``const`` .. code-block:: c const a = { .x = 10, .y = 20 }; * Step 3 : Remove everything before ``const`` .. code-block:: c const a = { .x = 10, .y = 20 }; * Step 4 : Remove assignment .. code-block:: c const a; * Step 5 : Now apply the rule. Anything after ``const`` keyword CAN NOT be changed .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * We see ``a`` after ``const`` * Means, variable ``a`` CAN NOT be changed again in next line .. tab-set:: .. tab-item:: Example 1 : Snippet 1 .. code-block:: c struct ABC { int x; int y; }; const struct ABC a = { .x = 10, .y = 20 }; a.x = 100; // --> This is invalid a.y = 200; // --> This is invalid .. tab-set:: .. tab-item:: Example 1 : Snippet 2 .. code-block:: c struct ABC { int x; int y; }; const struct ABC a = { .x = 10, .y = 20 }; struct ABC b = { .x = 100, .y = 200 }; a = b; // --> This is invalid .. _const_ptr_struct_sp_ex_2: .. tab-set:: .. tab-item:: Example 2 : const struct ABC \*p = &a; * Step 1 : Consider the statement .. code-block:: c const struct ABC *p = &a; * Step 2 : Remove all keywords after ``const`` .. code-block:: c const *p = &a; * Step 3 : Remove everything before const .. code-block:: c const *p = &a; * Step 4 : Remove assignment .. code-block:: c const *p; * Step 5 : Now apply the rule. Anything after ``const`` keyword CAN NOT be changed .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * We see ``*p`` after ``const`` * Means, ``*p`` CAN NOT be changed again in next line .. code-block:: c struct ABC { int x; int y; }; struct ABC a = { .x = 10, .y = 20 }; const struct ABC *p = &a; p->x = 100; // --> This is invalid p->y = 200; // --> This is invalid * Step 6 : Bonus point ! ``*p`` and ``p`` are different. Means you can change ``p`` * From Step 4, we derived that "const \*p" * Means only ``*p`` is const and CAN NOT be changed * But remember ``p`` is entirely different * There is nothing which says ``p`` is constant * Hence ``p`` can still be changed * See below example .. code-block:: c struct ABC a , b; const struct ABC *p = &a; p = &b; // --> This is valid .. _const_ptr_struct_sp_ex_3: .. tab-set:: .. tab-item:: Example 3 : struct ABC \*const p = &a; * Step 1 : Consider the statement .. code-block:: c struct ABC *const p = &a; * Step 2 : Remove all keywords after ``const`` .. code-block:: c struct ABC *const p = &a; * Step 3 : Remove everything before const .. code-block:: c const p = &a; * Step 4 : Remove assignment .. code-block:: c const p; * Step 5 : Now apply the rule. Anything after ``const`` keyword CAN NOT be changed .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * We see ``p`` after ``const`` * Means, ``p`` CAN NOT be changed again in next line .. code-block:: c struct ABC a, b; struct ABC *const p = &a; p = &b; // --> This is invalid * Step 6 : Bonus point ! ``*p`` and ``p`` are different. Means you can change ``*p`` * From Step 4, we derived that "const p" * Means only ``p`` is const and CAN NOT be changed * But remember ``*p`` is entirely different * There is nothing which says ``*p`` is constant * Hence ``*p`` can still be changed * See below example .. code-block:: c struct ABC a, b; struct ABC *const p = &a; p->x = 100; // --> This is valid p->y = 200; // --> This is valid .. _const_ptr_struct_sp_ex_4: .. tab-set:: .. tab-item:: Example 4 : const struct ABC \*const p = &a; * There are two occurences of ``const`` keyword * Hence, let us apply the same rules two times .. tab-set:: .. tab-item:: 4.1 : Rules w.r.t first const * Step 1 : Consider the statement .. code-block:: c const struct ABC *const p = &a; * Step 2 : Remove all keywords after first const .. code-block:: c const *p = &a; * Step 3 : Remove everything before first const .. code-block:: c const *p = &a; * Step 4 : Remove assignment .. code-block:: c const *p; * Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*p CAN NOT be changed .. tab-set:: .. tab-item:: 4.2 : Rules w.r.t second const * Step 1 : Consider the statement .. code-block:: c const struct ABC *const p = &a; * Step 2 : Remove all keywords after second const .. code-block:: c const struct ABC *const p = &a; * Step 3 : Remove everything before second const .. code-block:: c const p = &a; * Step 4 : Remove assignment .. code-block:: c const p; * Step 5 : Now apply the rule. Anything after const keyword CAN NOT be changed .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow p CAN NOT be changed .. tab-set:: .. tab-item:: Summary of Example 4 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Both p and \*p CAN NOT be changed .. code-block:: c struct ABC a, b; const struct ABC *const p = &a; p->x = 100; // --> This is invalid p->y = 200; // --> This is invalid p = &b; // --> This is invalid .. _const_ptr_struct_sp_ex_5: .. tab-set:: .. tab-item:: Summary ============================== ==================================================== Statement Meaning ============================== ==================================================== const struct ABC a; * ``a`` CAN NOT be changed again struct ABC const a; * ``a`` CAN NOT be changed again const struct ABC \*p; * ``*p`` CAN NOT be changed * ``p`` can be changed struct ABC const \*p; * ``*p`` CAN NOT be changed * ``p`` can be changed struct ABC \*const p; * ``*p`` can be changed * ``p`` CAN NOT be changed const struct ABC \*const p; * ``*p`` CAN NOT be changed * ``p`` CAN NOT be changed ============================== ==================================================== .. card:: See Also * Current Module * :doc:`../const_ptr` * Previous Module * :doc:`../../memcpy_ptr/memcpy_ptr` * Next Module * :doc:`../../void_ptr/void_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:`../../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`