const int 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 int a = 5;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``int const a = 5;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``const int *p = &a;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``int const *p = &a;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``int *const p = &a;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``const int *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 int a = 5; ` * :ref:`Example 2 : const int *p = &a; ` * :ref:`Example 3 : int *const p = &a; ` * :ref:`Example 4 : const int *const p = &a; ` * :ref:`Summary ` .. _const_ptr_int_sp_ex_1: .. tab-set:: .. tab-item:: Example 1 : const int a = 5; * Step 1 : Consider the statement .. code-block:: c const int a = 5; .. plantuml:: :const int a = 5; fork :const; kill fork again :int; kill fork again :a = 5; endfork kill * Step 2 : Remove all keywords after ``const`` .. code-block:: c const a = 5; .. plantuml:: :const a = 5; fork :const; kill fork again :a = 5; endfork kill * Step 3 : Remove everything before ``const`` .. code-block:: c const a = 5; * Step 4 : Remove assignment .. code-block:: c const a; .. plantuml:: :const a; fork :const; kill fork again :a; endfork kill * 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 .. code-block:: c const int a = 5; a = 10; // --> This is invalid .. _const_ptr_int_sp_ex_2: .. tab-set:: .. tab-item:: Example 2 : const int \*p = &a; * Step 1 : Consider the statement .. code-block:: c const int *p = &a; .. plantuml:: :const int *p = &a; fork :const; kill fork again :int; kill fork again : *p = &a; endfork kill * Step 2 : Remove all keywords after ``const`` .. code-block:: c const *p = &a; .. plantuml:: :const *p = &a; fork :const; kill fork again : *p = &a; endfork kill * Step 3 : Remove everything before const .. code-block:: c const *p = &a; * Step 4 : Remove assignment .. code-block:: c const *p; .. plantuml:: :const *p; fork :const; kill fork again : *p; endfork kill * 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 int a = 5; const int *p = &a; *p = 10; // --> 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 int a = 5, b = 6; const int *p = &a; printf("*p is %d\n", *p); // prints 5 p = &b; // --> This is valid printf("*p is %d\n", *p); // prints 6 .. _const_ptr_int_sp_ex_3: .. tab-set:: .. tab-item:: Example 3 : int \*const p = &a; * Step 1 : Consider the statement .. code-block:: c int *const p = &a; .. plantuml:: :int *const p = &a;; fork :int *; kill fork again :const; kill fork again : p = &a; endfork kill * Step 2 : Remove all keywords after ``const`` .. code-block:: c int *const p = &a; * Step 3 : Remove everything before const .. code-block:: c const p = &a; .. plantuml:: :const p = &a; fork :const; kill fork again : p = &a; endfork kill * Step 4 : Remove assignment .. code-block:: c const p; .. plantuml:: :const p; fork :const; kill fork again : p; endfork kill * 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 int a = 5, b = 6; int *const p = &a; printf("*p is %d\n", *p); // prints 5 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 int a = 5, b = 6; int *const p = &a; printf("*p is %d\n", *p); // prints 5 *p = 100; // --> This is valid printf("*p is %d\n", *p); // prints 100 .. _const_ptr_int_sp_ex_4: .. tab-set:: .. tab-item:: Example 4 : const int \*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 int *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 int *const p = &a; * Step 2 : Remove all keywords after second const .. code-block:: c const int *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 int a = 5, b = 6; const int *const p = &a; *p = 100; // --> This is invalid p = &b; // --> This is invalid .. _const_ptr_int_sp_ex_5: .. tab-set:: .. tab-item:: Summary ============================== ==================================================== Statement Meaning ============================== ==================================================== const int a = 5; * ``a`` CAN NOT be changed again int const a = 5; * ``a`` CAN NOT be changed again const int \*p = &a; * ``*p`` CAN NOT be changed * ``p`` can be changed int const \*p = &a; * ``*p`` CAN NOT be changed * ``p`` can be changed int \*const p = &a; * ``*p`` can be changed * ``p`` CAN NOT be changed const int \*const p = &a; * ``*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`