| int x; function()=x; Is it... | |||||||
|
30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied. Book (308 Pages):
![]() Video (One Hour):
![]() Resume Review (24 - 48hr)
All Products / Services
|
|||||||
It is illegal. They are probably looking for a understanding of lvalue and rvalue (in essence what can and cannot appear on the left hand side of an assignment).
It can be legal if 'function()' returns a reference to an integer
And u shud hold the reference somewhere...
I agree with haoguohua
I tried to declare function as "int * function" or "int * *function", both don't work..
anyone knows it?
done, I get it:
int & function()
{
int * newint = (int *)malloc(sizeof(int));
return (int &) *newint;
}
this could through g++, and also can print out the number,
for sure it could be legal.
===================================
int gg=0;
int& function()
{
return gg;
}
int main()
{
function()=6;
std::cout<<gg<<endl;
return 0;
}
and it will print a 6
I don't think this could be done in C.References were introduced in C++ only,there were no references(&) in C.
I don't think, interviewer asked this question to check your understanding on reference......He seems to be concerned with lvalue and rvalue but its make sense to mention about reference after mentioned about lvalue and rvalue........
It suffices to give a correct answer. You need not guess the concerns of the interviewer and give an appropriate answer.
int global = 500;
int * function()
{
return &global;
}
int main()
{
*(function())= 1000;
printf("{%d}\n",*function());
getch();
return 0;
}
The above program prints 1000 as expected.
You have changed the problem statement.
The original statement was function()=x. It makes a world of difference by putting the *
illegal ... left side of the assignment operator expected to a variable.