‘const’ keyword and pointers in C++ (CPP) language

Many a times one of the challenges a beginner faces is with the use of const keyword. It comes handy when one understands the exact use of it. Const keyword changes its meaning depending on where it is being used. Below I give all possible use of the keyword.

Normal integer variable:
           int intValue                                                                            // integer variable
           As function parameter:
                       Void testFunc(int intValue) {
                                   // modification of intValue is possible
                                   intValue = 10;
                       }
                       Here the value passed to testFunc() is copied to a separate memory area, which can be accessed through intValue

Pointer to integer variable:
           int *ptrToInt                                                              // pointer to integer variable
           As function parameter:
                       void testFunc(int *ptrToInt) {
                                   int a = 10;
                                   cout << ptrToInt << endl;               //prints the memory address of passed variable
                                   cout << *ptrToInt << endl;

                                   ptrToInt = &a;                                               //assigning address of variable ‘a’ to ptrToInt ie. Modification of ptrToInt is possible

                                   cout << ptrToInt << endl;               //prints the memory address of variable a
                                   cout << *ptrToInt << endl;  //prints the value of a ie. 10
                       }

Constant integer value:
           int const constInt                                                      // constant variable, cant be modified
          const int constInt                                                      // also
           As function parameter:
                       void testFunc(int const constInt) {
                                   //constInt can’t be changed here
                       }

Pointer to constant integer:
           int const *ptrToConstInt                                          // pointer to a constant integer variable
          const int *ptrToConstInt                                          // also
           As function parameter:
                       void testFunc(const int *ptrToConstInt) {
                               const int a = 10;
                                   ptrToConstInt = &a;
                                   cout << ptrToConstInt << endl;               // prints the memory address of variable a
                               cout << *ptrToConstInt << endl;            // prints the value of a ie. 10
                       }

Constant pointer to an integer:
           int *const constPtrToInt                                           // constant pointer to integer variable
           As function parameter:
                       void testFunc(int *const constPtrToInt) {
                                   //constPtrToInt cant be changed but the value it points to, can be altered
                                   *constPtrToInt += 10;                 //since it points to, is a normal integer value
                       }

Constant pointer to constant integer:
           int const * const constPtrToConstInt          // constant pointer to constant integer value
           As function parameter:
                       void testFunc(int const *const constPtrToConstInt) {
                                   //here the value of constPtrToConstInt as well as the value it points to, both is immutable
                       }

MORE USE OF ‘const’ KEYWORD:

Constant pointer to a constant pointer to an integer:
           int * const * const const_ptr_to_const_ptr_to_int

Constant pointer to a pointer to a pointer:
           int ** const const_ptr_to_ptr_to_int

Pointer to a constant pointer to an integer:
           int * const * ptr_to_const_ptr_to_int

Pointer to a pointer to an integer:
           int ** ptr_to_ptr_to_int

Tip: “int const” and “const int” are same. So if the line contains any of these two, understand that the final part is “integer constant” take the rest of the line and read backwards,
                       *                     ==means==>         ‘pointer to’
                       * const            ==means==>         ‘constant pointer to’
                       Multiple ‘*’        ==means==>         that number of ‘pointer to’

 

Hope this post was helpful to you… 🙂

Published by

Sreejith

A strong believer of: 1. Knowledge is power 2. Progress comes from proper application of knowledge 3. Reverent attains wisdom 4. For one's own salvation, and for the welfare of the world

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s