const int double 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 **q;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``const int *const *q;`` ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow What is ``const int *const *const q;`` ? * 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 **q; ` * :ref:`Example 2 : const int *const *q; ` * :ref:`Example 3 : const int *const *const q; ` * :ref:`Summary ` .. _const_ptr_int_dp_ex_1: .. tab-set:: .. tab-item:: Example 1 : const int \*\*q; * Step 1 : Consider the statement .. code-block:: c const int **q; * Step 2 : Remove all keywords after ``const`` .. code-block:: c const **q; * Step 3 : Remove everything before ``const`` .. code-block:: c const **q; * Step 4 : Remove assignment .. code-block:: c const **q; .. tab-set:: .. tab-item:: Example 1 : Snippet 1 * 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 ``**q`` after ``const`` * Means, ``**q`` CAN NOT be changed again in next line .. code-block:: c int a = 5; int *p; const int **q; p = &a; q = &p; **q = 100; // --> This is invalid .. tab-set:: .. tab-item:: Example 1 : Snippet 2 * Step 6 : Bonus point ! q, \*q, \*\*q are different .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Means, ``q`` and ``*q`` can still be changed .. code-block:: c int a = 5, b = 6; int *x, *y; const int **q; x = &a; q = &x; // --> This is valid y = &b; *q = y; // --> This is valid printf("**q = %d\n", **q); // --> prints 6 .. _const_ptr_int_dp_ex_2: .. tab-set:: .. tab-item:: Example 2 : const int \*const \*q; * There are two occurences of const keyword * Hence, let us apply the same rules two times .. tab-set:: .. tab-item:: 2.1 : Rules w.r.t first const * Step 1 : Consider the statement .. code-block:: c const int *const *q; * Step 2 : Remove all keywords after first const .. code-block:: c const **q; * Step 3 : Remove everything before first const .. code-block:: c const **q; * Step 4 : Remove assignment .. code-block:: c const **q; * 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 \*\*q CAN NOT be changed .. tab-set:: .. tab-item:: 2.2 : Rules w.r.t second const * Step 1 : Consider the statement .. code-block:: c const int *const *q; * Step 2 : Remove all keywords after second const .. code-block:: c const int *const *q; * Step 3 : Remove everything before second const .. code-block:: c const *q; * Step 4 : Remove assignment .. code-block:: c const *q; * 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 \*q CAN NOT be changed .. tab-set:: .. tab-item:: Summary of Example 2 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * \*\*q, \*q CAN NOT be changed * q can be changed .. _const_ptr_int_dp_ex_3: .. tab-set:: .. tab-item:: Example 3 : const int \*const \*const q; * There are three occurences of const keyword * Hence, let us apply the same rules three times .. tab-set:: .. tab-item:: 3.1 : Rules w.r.t first const * Step 1 : Consider the statement .. code-block:: c const int *const *const q; * Step 2 : Remove all keywords after first const .. code-block:: c const **q; * Step 3 : Remove everything before first const .. code-block:: c const **q; * Step 4 : Remove assignment .. code-block:: c const **q; * 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 * \*\*q CAN NOT be changed .. tab-set:: .. tab-item:: 3.2 : Rules w.r.t second const * Step 1 : Consider the statement .. code-block:: c const int *const *const q; * Step 2 : Remove all keywords after second const .. code-block:: c const int *const *q; * Step 3 : Remove everything before second const .. code-block:: c const *q; * Step 4 : Remove assignment .. code-block:: c const *q; * 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 \*q CAN NOT be changed .. tab-set:: .. tab-item:: 3.3 : Rules w.r.t third const * Step 1 : Consider the statement .. code-block:: c const int *const *const q; * Step 2 : Remove all keywords after third const .. code-block:: c const int *const *const q; * Step 3 : Remove everything before third const .. code-block:: c const q; * Step 4 : Remove assignment .. code-block:: c const q; * 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 * q CAN NOT be changed .. tab-set:: .. tab-item:: Summary of Example 3 .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow \*\*q, \*q, q CAN NOT be changed .. _const_ptr_int_dp_ex_4: .. tab-set:: .. tab-item:: Summary ============================== ==================================================== Statement Meaning ============================== ==================================================== const int \*\*q; * ``**q`` CAN NOT be changed again * ``*q`` can be changed again * ``q`` can be changed again const int \*const \*q; * ``**q`` CAN NOT be changed again * ``*q`` CAN NOT be changed again * ``q`` can be changed again const int \*const \*const q; * ``**q`` CAN NOT be changed again * ``*q`` CAN NOT be changed again * ``q`` CAN NOT be changed again ============================== ==================================================== .. 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`