Skip to content
Related Articles

Related Articles

C | Storage Classes and Type Qualifiers | Question 19
  • Difficulty Level : Basic
  • Last Updated : 09 Feb, 2013




#include <stdio.h>
int main()
{
    int x = 5;
    int * const ptr = &x;
    ++(*ptr);
    printf("%d", x);
    
    return 0;
}


(A) Compiler Error
(B) Runtime Error
(C) 6
(D) 5


Answer: (C)

Explanation: See following declarations to know the difference between constant pointer and a pointer to a constant.
int * const ptr —> ptr is constant pointer. You can change the value at the location pointed by pointer p, but you can not change p to point to other location.
int const * ptr —> ptr is a pointer to a constant. You can change ptr to point other variable. But you cannot change the value pointed by ptr.
Therefore above program works well because we have a constant pointer and we are not changing ptr to point to any other location. We are only icrementing value pointed by ptr.


Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :