Pointer, Virtual Function & Polymorphism

Describe how the const keyword can be used with pointers?

First we will discuss how we should read the pointers. It should read from right to left (clockwise)

Sr. No.StatementsDescription
1int * ptrptr is a pointer to int
2intconst *ptrptr is a pointer to constint
3int * constptrptr is a const pointer to int
4intconst * constptrptr is a const pointer to constint


Case 1: int * ptr ------------------------ ptr is a pointer to int
void main()
{
     int a = 100;
     int *ptr;
     ptr = &a;
     cout<<"Value of a =: "<<a<<endl;
     cout<<"Value of variable a through pointer ptr=: "<<*ptr<<endl;
     cout<<"Address of a =: "<<&a<<endl;
     cout<<"Address of a through pointer ptr=: "<<ptr<<endl;
}

Here pointer ptr is non-constant so it cannot store the address of const variable. If we declare int a as constint a, then it will give error.
intconst a = 120;
int *ptr = &a; //this is invalid

Case 2: intconst *ptr ----------------------------- ptr is a pointer to constint
void main()
{
     int a = 100;
     constint *ptr;
     ptr = &a;
     cout<<a<<endl;
     cout<<*ptr<<endl;
     cout<<&a<<endl;
     cout<<ptr<<endl;
     a = 200;
}

In the above given example, pointer ptr is pointed to integer constant and int a is non-const. Although int a is non-const in nature, still pointer ptr is assuming it as const and hence does not allow us to change its value through it (i.e.ptr).

Now let us consider some other things in this case.

A. Can we perform *++ptr.

Yes, You can execute this statement, it will give some garbage value. ++ptr will execute first due to operator precedence, then * will be applied on the corresponding value

B. Can we perform ++ (*ptr).

No, If you execute ++ (*ptr) statement it will give the error. ++ (*ptr) statement is trying to modify the content of variable a.

C. Can we perform *ptr++.

Yes, You can execute *ptr++

Example:
int a = 10;
int *ptr = &a;
int x = *ptr++;

This statement will be evaluated and executed by the compiler as:
x = *ptr;
ptr = ptr + 1;

So, after first statement, 'x' will be having the value stored at ptr (i.e. of the variable to whom ptr is pointing). And then there would be increment in the value of ptr. So, now after second statement, ptr is no longer pointing to variable 'a'. It will be pointing to any other memory location.

constptr

In the above figure we are considering that address of ptr, and 'a' are 1200 and 300 respectively under 16 bit compiler.

Case 3: int * constptr ------------------------------------ ptr is a const pointer to int

const pointer pointing to non-const variable
void main()
{
     int a = 100;
     int b = 200;
     int * constptr = &a; // ptr is a constant pointer to the integer
     cout<<a<<endl;
     cout<<*ptr<<endl;
     cout<<&a<<endl;
     cout<<ptr<<endl;
     cout<<(*ptr)++;
     // ptr = &b; ----------------------------- It will give error.
}

Now let us consider some more things in this case.

A. Can we perform ptr = &b;

No, You cannot store address of any other variable because ptr is a constant pointer to the integer. it is loyal to address of "int a" only.

B. Can we perform *ptr++

No, It will give error. we can't assign any other address to ptr except int a. ++ has higher precedence than *, so ptr++ evaluates first, which leads to point next location of the memory, which is illegal as ptr is " constptr" in nature.

C. Can we perform (*ptr)++;

Yes, You can execute (*ptr)++; here we are incrementing value of a through ptr. Brackets are having highest precedence, so *ptr get evaluated first and then there is an increment in its value.

Case: 4 intconst * constptr ----------------------------- ptrconstptr pointing to constint

In this case ptr is constant pointer to constant variable. Here both pointer and variable are constant in nature which means we cannot change value pointed by pointer as well as we cannot point the pointer to other variable.

Example:
void main()
{
     int a = 100;
     int b = 200;
     constint * constptr = &a;
     cout<<a<<endl;
     cout<<*ptr<<endl;
     cout<<&a<<endl;
     cout<<ptr<<endl;
}

We cannot perform the following operation (Consider above example)
A. ptr = &b;

It will give error. we can't assign any other address to ptr except int a.
B. *(ptr++)
C. ++(*ptr)
D. ++(*ptr)

2. Write output with explanation.

void main ( )
{
     int a, *b = &a;
     int **c = &b;
     a = 2 ;
     **c = 4;
     b = (int*) **c;
     cout<<"A="<<a<<endl;
     cout<<"B="<<b;
}

Description:

The program will not compile. Compiler scan (int a, *b = &a;) this statement from right to left and at starting compiler will not know the address of variable “a”. If you write one line statement ( int a, *b = &a; ) as given below then it will be OK.
int a;
int *b = &a;