I was trying to pass reference to pointers, mainly two cases:
const int* &ref_var- ref_var being a reference to a pointer to a constant integer.int *const &ref_var- ref_var being a reference to a constant pointer to an integer.
Now the second case works fine since I cannot modify the pointer itself, but in 1st case I am receiving an error message which I don't think should be there since a non-const pointer variable can be passed as argument to a function with constant pointer as parameter.
code:
#include<iostream>
using std::cout;
using std::endl;
void fun(const int* &ref_var) //error: invalid initialization of reference of type const int*& from expression of type int*
{
//BLANK
}
int main()
{
int x=9;
int *p=&x; //ERROR VANISHES IF P IS DECLARED AS POINTER TO CONSTANT INT
fun(p);
return 0;
}
Where as when the function fun is declared as:
void fun(const int *ptr)
{
//BLANK
}
It works fine despite the argument passed being non-const pointer to int.
Could you please tell me where am I going wrong.