C const Pointer

Constant Pointers

A constant pointer is a pointer that cannot change the address of the variable to which it is pointing. So once a constant pointer points to a variable then it cannot point to any other variable.

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

Declaration of a constant pointer

snippet
int *const ptr;

Let's understand the constant pointer through an example.

snippet
#include <stdio.h>
int main()
{
    int a=1;
    int b=2;
    int *const ptr;
    ptr=&a;
    ptr=&b;
    printf("Value of ptr is :%d",*ptr);
    return 0;
}

In the above code.

  • We declare two variables, i.e., a and b with values 1 and 2, respectively.
  • We declare a constant pointer.
  • First, we assign the address of variable 'a' to the pointer 'ptr'.
  • Then, we assign the address of variable 'b' to the pointer 'ptr'.
  • Lastly, we try to print the value of the variable pointed by the 'ptr'.
const Pointer in C

In the above output, we can observe that the above code produces the error "assignment of read-only variable 'ptr'". It means that the value of the variable 'ptr' which 'ptr' is holding cannot be changed. In the above code, we are changing the value of 'ptr' from &a to &b, which is not possible with constant pointers. Therefore, we can say that the constant pointer, which points to some variable, cannot point to another variable.

Pointer to Constant

Pointer to constant is a pointer that restricts modification of value pointed by the pointer. The address of these pointers can be changed, but we cannot modify the value pointed by pointer.

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

Declaration of a pointer to constant

snippet
const int* ptr;

Let's understand through an example.

First, we write the code where we are changing the value of a pointer

snippet
#include <stdio.h>
int main()
{
    int a=100;
    int b=200;
    const int* ptr;
    ptr=&a;
    ptr=&b;
    printf("Value of ptr is :%u",ptr);
    return 0;
}

In the above code.

  • We declare two variables, i.e., a and b with the values 100 and 200 respectively.
  • We declare a pointer to constant.
  • First, we assign the address of variable 'a' to the pointer 'ptr'.
  • Then, we assign the address of variable 'b' to the pointer 'ptr'.
  • Lastly, we try to print the value of 'ptr'.
const Pointer in C

The above code runs successfully, and it shows the value of 'ptr' in the output.

Now, we write the code in which we are changing the value of the variable to which the pointer points.

snippet
#include <stdio.h>
int main()
{
    int a=100;
    int b=200;
    const int* ptr;
    ptr=&b;
    *ptr=300;
    printf("Value of ptr is :%d",*ptr);
    return 0;
}

In the above code.

  • We declare two variables, i.e., 'a' and 'b' with the values 100 and 200 respectively.
  • We declare a pointer to constant.
  • We assign the address of the variable 'b' to the pointer 'ptr'.
  • Then, we try to modify the value of the variable 'b' through the pointer 'ptr'.
  • Lastly, we try to print the value of the variable which is pointed by the pointer 'ptr'.

Output

const Pointer in C

The above code shows the error "assignment of read-only location '*ptr'". This error means that we cannot change the value of the variable to which the pointer is pointing.

Constant Pointer to a Constant

A constant pointer to a constant is a pointer, which is a combination of the above two pointers. It can neither change the address of the variable to which it is pointing nor it can change the value placed at this address.

Syntax
const <type of pointer>* const <name of the pointer>;

Declaration for a constant pointer to a constant

snippet
const int* const ptr;

Let's understand through an example.

snippet
#include <stdio.h>
int main()
{
    int a=10;
    int b=90;
    const int* const ptr=&a;
   *ptr=12;
    ptr=&b;
    printf("Value of ptr is :%d",*ptr);
    return 0;
}

In the above code.

  • We declare two variables, i.e., 'a' and 'b' with the values 10 and 90, respectively.
  • We declare a constant pointer to a constant and then assign the address of 'a'.
  • We try to change the value of the variable 'a' through the pointer 'ptr'.
  • Then we try to assign the address of variable 'b' to the pointer 'ptr'.
  • Lastly, we print the value of the variable, which is pointed by the pointer 'ptr'.
const Pointer in C

The above code shows the error "assignment of read-only location '*ptr'" and "assignment of read-only variable 'ptr'". Therefore, we conclude that the constant pointer to a constant can change neither address nor value, which is pointing by this pointer.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +