Explain About Constant Pointer and Pointer to Constant

1. constant pointer :-

Constant Pointer mean we can't change the address of Pointer but we can change the value.That is Pointer is pointing to constant address.

 Syntax:-
  int* const ptr    

#include<stdio.h>
int  main()
{
    int num=10;
    int* const ptr = &num;
   int *anotherptr;
   int number=1000;
   printf("The before num value is:%d\n",*ptr);
  *ptr = number;
   //ptr=anotherptr; because we can't change the address of ptr
   printf("The num after value is:%d\n",*ptr);
  return(0);
}

2.pointer to constant :-

 Pointer to Constant means we can't change the value but we can change the address of Pointer. That is value is constant.

Syntax:-
 const int *ptr;


 #include<stdio.h>    
int  main()
{
     int num=3;
    const int *ptr = &num;
    int *AnotherPtr;
    int a=1000;
    Anotherptr=&a;
    printf("Before:The num value is:%d\n",*ptr);
   //*ptr = 5; we can't change the value because it's const
    ptr=Anotherptr;
    printf("After :The num value is:%d\n",*ptr);
   return(0);
}

1 comments:

ram
14 May 2013 at 03:00 comment-delete

thank you very much for nice and beautiful explanation ,because my teacher did not explain me about constant points

Reply

Post a Comment

Don't Forget to comment