![](sad.gif)
Explain Please
bool f(int &j)
{
return 0 = &j
}
Could the function f ever return true when called in a
C++ program? If not, explain why. If so, give a code
fragment which shows an example where f would return
true.
This was an interview question .. and this dumb me could not understand it
![](sad.gif)
VOID *NullSpace
Well...are you sure it's supposed to say "return 0 = &j" and not "return 0 == &j" ?
if what you have written is correct, it shouldn't even compile...
you can't assign a value to 0.
However, if the other way is correct, then it would return true if the memory address of j was == to null. However, I don't know if you can have a variable w/ a memory address of 0x0, but I may be wrong.
If I am wrong on this, will someone please correct me?
/**************************************
* Hookt on Fonix relly werked fer mee!
* WarAmp
***************************************/
Edited by - Waramp on April 5, 2001 2:41:17 AM
if what you have written is correct, it shouldn't even compile...
you can't assign a value to 0.
However, if the other way is correct, then it would return true if the memory address of j was == to null. However, I don't know if you can have a variable w/ a memory address of 0x0, but I may be wrong.
If I am wrong on this, will someone please correct me?
/**************************************
* Hookt on Fonix relly werked fer mee!
* WarAmp
***************************************/
Edited by - Waramp on April 5, 2001 2:41:17 AM
Waramp.Before you insult a man, walk a mile in his shoes.That way, when you do insult him, you'll be a mile away, and you'll have his shoes.
I agree WarAmp. The code as is should produce an error that says something to the effect of 'can't assign &j to '0', lvalue is required'..
Testing in MSVC gives:
"cannot convert from 'class glExtManager *' to 'const int'. This conversion requires a reinterpret_cast, a C-style cast or function-style cast".
If I provide the C-style cast (like so: 'return 0 = (int)&j') then I get this:
"left operand must be l-value"
EDIT: If it is supposed to be 'return 0 == &j', then it should be impossible. Since the parameter definition for j is 'int& j' that means it is passed by reference. Checking the address of 'j' will _never_ be null because in C++ it is not possible to have a NULL reference.
--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
Edited by - Promiscuous Robot on April 5, 2001 2:59:17 AM
Testing in MSVC gives:
"cannot convert from 'class glExtManager *' to 'const int'. This conversion requires a reinterpret_cast, a C-style cast or function-style cast".
If I provide the C-style cast (like so: 'return 0 = (int)&j') then I get this:
"left operand must be l-value"
EDIT: If it is supposed to be 'return 0 == &j', then it should be impossible. Since the parameter definition for j is 'int& j' that means it is passed by reference. Checking the address of 'j' will _never_ be null because in C++ it is not possible to have a NULL reference.
--------------------------
I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
Edited by - Promiscuous Robot on April 5, 2001 2:59:17 AM
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Yes. It can return true. What you do is you pass it a pointer. See my code, here.
main ()
{
int *q=new int;
q=NULL;
cout << f(*q) << endl;
}
bool f is already given.
Heh.. and I''m new to this. The reason this works is:
You are setting what q is pointing to to null. You pass the pointer with the *, you are talking about the item. The address of the item (which is non-existant) , is NULL. The comparison returns true, and life goes on.
Magnwa
main ()
{
int *q=new int;
q=NULL;
cout << f(*q) << endl;
}
bool f is already given.
Heh.. and I''m new to this. The reason this works is:
You are setting what q is pointing to to null. You pass the pointer with the *, you are talking about the item. The address of the item (which is non-existant) , is NULL. The comparison returns true, and life goes on.
Magnwa
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement