Difference between const int*, const int * const, and int const *

In this article, I will explain the difference between const int*, const int * const, and int const *. If you are learning C programming and the following questions are coming to your mind. For example,

  • What is the difference between const int * and int * const?
  • What is the difference between const int*, const int * const, and int const *?
  • What is the difference between int * const and const int *?
  • What is the difference between a constant pointer and a pointer constant?
  • What do you understand from this const int * ptr?
  • What is true for constant pointer and pointer to constant?

If your answer is “YES”, then great news for you. you are at the right place we will clear your all doubts regarding the above-mentioned questions. It is my recommendation that you must read the prerequisite articles if your concept is not clear on constant and pointers.

Prerequisite of the mentioned  Questions:

 

1. const int*:

Let’s take an example to understand this declaration.

const int *ptr;

Now Question is, What is ptr?

So if you have read the Clockwise/Spiral Rule, you can easily give the answer “ptr is a pointer to integer const“.

It is clear from the name that ptr is a pointer and it is pointing to a constant integer. Effectively, this implies that the pointer is pointing to a value that shouldn’t be changed.

In this declaration, the const qualifier doesn’t affect the pointer, so the pointer is allowed to point to some other address. In other, we can assign other addresses to the pointer.

The following example describes the pointer to const integer (const int *).

Example 1:

#include <stdio.h>
int main()
{
    //Integer variable
    int data = 2;

    //Assign the address of data
    const int *ptr = &data;

    ////Compilation error
    *ptr = 3;

    printf("*ptr is %d",*ptr);

    return 0;
}

Output:

error: assignment of read-only location ‘*ptr’

 

Example 2:

#include <stdio.h>
int main()
{
    //Integer variable
    int data = 2;

    int data1 = 3;

    //Assign the address of data
    const int *ptr = &data;

    //Valid
    ptr = &data1;

    printf("*ptr is %d",*ptr);

    return 0;
}

Output: 

*ptr is 3

 

Note✍ The const int * is equivalent to the int const *.

 

✌Directly Jump to Video Explanation in Hindi

 

2. int *const:

Consider the below example,

int *const ptr;

Now again the same question is, What is ptr?

I believe till now you have read the Clockwise/Spiral Rule, so you can easily say that ptr is a constant pointer to an integer.

It is clear from the name that ptr is itself a constant (not modifiable ) which is pointing to an integer. That means the pointer shouldn’t point to some other address. Const qualifier doesn’t affect the pointed integer. So allowed to change the value of the pointed integer.

The following example describes the constant pointer to integer variable (int * const).

Example 1:

#include <stdio.h>
int main()
{
    //Integer variable
    int data = 2;

    //Assign the address of data
    int * const ptr = &data;

    //valid
    *ptr = 3;

    printf("data is %d",data);

    return 0;
}

Output:

data is 3

 

Example 2:

#include <stdio.h>
int main()
{
    //Integer variable
    int data1 = 2;

    int data2 = 5;

    //Assign the address of data
    int * const ptr = &data1;

    //compiler error
    ptr = &data2;

    return 0;
}

Output:

Compile-time error.

 

3. const int * const:

Again let’s see an example to understand this declaration,

const int * const ptr;

I will not again ask the same question because you already know what ptr is. But still, you are not able to prase this declaration then I am begging please read Clockwise/Spiral Rule, here ptr

So if you have read the Clockwise/Spiral Rule, you can easily give the answer “ptr is a constant pointer to an integer const”.

It is clearly understandable from the parsing that const qualifier affects pointer and pointed variable both. It means that the ptr is here a constant pointer that is pointing to a constant integer. Hence, neither the ptr should point to a new address nor the value being pointed to should be changed.

The following example describes the constant pointer to the constant integer variable (const int * const).

Example:

#include <stdio.h>

int main()
{
    const int data1 = 5;

    //Valid
    const int* const ptr = &data1;

    //Compilation error
    *ptr = 26;

    const int data2 = 7;

    //Compilation error
    ptr = &data2;

    return 0;
}

Output:

Compile-time error

 

Now I believe you have a better understanding of the declarations, const int*, const int * const, and int const *, so now let’s see the differences between these declarations. See the below comparison chart.

Difference between const int*, const int * const, and int const *

 

 

Difference between const int*, const int * const, and int const * with Video (Hindi):

 

 

Recommended Post

Leave a Reply

Your email address will not be published. Required fields are marked *