parag_p...@hotmail.com wrote:
#include <iostream>
using namespace std;
void ha(int& j);
void ha(int& j) {
cout <<j<<endl;
}
main()
int main()
{
int j = 0;
ha(NULL);
}
Why so ,
gcc version 3.3.6
p236.cc: In function `int main()':
p236.cc:14: error: invalid initialization of non-const reference of
type 'int&'
from a temporary of type 'long int'
p236.cc:6: error: in passing argument 1 of `void ha(int
'NULL' is a substitute for and integral constant expression of
value 0. It is not an lvalue. In C++ a non-const reference
(parameter of 'ha') can only be bound to an lvalue. 'NULL' is not
one, which is why you can't call your 'ha' function with 'NULL'
argument. Either specify an lvalue as an argument
int j = 0;
ha(j); // OK
or make your 'ha' accept a const reference
void ha(const int& j) {
cout <<j<<endl;
}
...
ha(NULL); // OK
It is worth noting though, that NULL macro should normally be used
in pointer context and using it in place of an 'int' value is not a
good practice.
--
Best regards,
Andrey Tarasevich
That helps thanks a lot.
I did not get the NULL macro.