top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Difference between constant pointer and pointer to constant ?

0 votes
343 views

chose

posted Sep 1, 2014 by Neelam

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Constant pointer : It means the pointer itself is constant i.e you can't point this pointer to some other variable.

A constant pointer is defined as : type * const pointer_name;

Lets see the example.

int main()
{
    int val1 = 10, val2 =  20;
    int * const ptr = &val1;
    /* "int * const ptr" this is called a  constant pointer. */
    ptr = &val2; 
    /* This line will produce error.  Because ptr is a constant pointer and hence can't point to another variable. */
    printf("%d\n",  *ptr);
    return 0;
}

Pointer to constant : can change the address they point to but cannot change the value kept at those address.

 A pointer to constant is defined as :    const type * pointer_name;

Lets see the example.

int main()

    int val = 10;
    const int * ptr = &val;
    /* "const int * ptr" this is called a pointer to  constant. */
    *ptr = 10; 
    /* This line will produce error.  Because ptr is a pointer to constant and hence can't change the value */ 
    printf("%d\n",  *ptr);
    return 0;
}
answer Sep 2, 2014 by Arshad Khan
0 votes

Constant pointer :
Given Pointer is constant, so we cant change the address.

Pointer to constant:
Pointer pointing to the value is constant, so we cant change the value

answer Dec 4, 2014 by sivanraj
Similar Questions
+1 vote

Also can we declare a variable as "constant volatile" ? if yes then what may be the possible reason to do so?

+1 vote

int arr[ ] = { 1, 2 };
p = arr; /* p is pointing to arr */

How pointer p will behave ?

...