Difference between constant pointer, pointer to constant and both pointer to constant and constant pointer

Constant Pointers:

A constant pointer is a pointer that cannot change the address its holding.

In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

Declaration of constant pointer:

<type of pointer> * const <name of pointer>

An example:

                int * const ptr;

An example program:

#include<stdio.h>

int main()

{

    int n1 = 0, n2 = 0;

    int *const ptr = &n1;

    *ptr = 100;//valid

    ptr = &n2;//Invalid

    printf("%d\n", *ptr);

}

In the above example:

We declared two variables n1 and n2 and constant pointer ‘ptr’ was declared and made to point n1. Next, ptr is made to point n2,   then print the value at ptr, but as per the constant pointer the pointer pointing an address cannot be change, so we will get the error mentioned below

“ error: assignment of read-only variable ‘ptr’

           ptr = &n2; ”

but we can change the value at the pointer.

Pointer to Constant:

A pointer to a constant is a pointer that cannot change the value of the address its holding through the pointer.

Declaration of pointer to a constant:

<type of pointer> const * <name of pointer>

                                    or

const <type of pointer>* <name of pointer>

An example:

                int const*ptr;

                                or

                const int *ptr;

An example program:

#include<stdio.h>

int main()

{

    int n1 = 0, n2 = 0;

    const int *ptr = &n1;

    *ptr = 100;//Invalid

    printf("%d\n", *ptr);//Invalid

    ptr = &n2;//valid

    printf("%d\n", *ptr);

}

In the above example:

                We declared two variables n1 and n2 and a pointer to constant ‘ptr’ was declared and made to point n1. Next, ptr is made to dereference, then print the value at ptr, but as per the pointer we cannot dereference, so we got the error mentioned below

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

     *ptr = 100;//Invalid

But we can change the address of the pointer.

Both pointer to constant and constant pointer:

                Both pointer to constant and constant pointer is a pointer that cannot change the address its holding and cannot dereference the value also.

Declaration of constant pointer:

const <type of pointer> * const <name of pointer>

An example:

                const int * const ptr;

An example program:

#include<stdio.h>

int main()

{

    int n1 = 20, n2 = 10;

    const int *const ptr = &n1;

    printf("%d\n", *ptr);//20

    *ptr = 100;//Invalid

    ptr = &n2;//Invalid

 

}

In the above example:

    We declared two variables n1 and n2 , here pointer to constant and constant pointer ‘ptr’ was declared and made to point n1. Next, ptr is made to point n2,   then print the value at ptr, and change the address and dereference the value, both are violating the rules, so we will get the error mentioned below

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

     *ptr = 100;//Invalid

          ^

 error: assignment of read-only variable ‘ptr’

     ptr = &n2;//Invalid ”

 

powered by social2s