Can I run this code in C .I have successfully run it in C++ but it gives me trouble in C
int & At()
{
int x;
return x;
}
Can I run this code in C .I have successfully run it in C++ but it gives me trouble in C
int & At()
{
int x;
return x;
}
>Can I run this code in C
No, C doesn't support references
-Prelude
My best code is written with the delete key.
You will need to use pointers, but even then the code will not work because the value that you are returning is that of a local variable that will go out of scope once the function exits. You would either want to pass a pointer to the variable you will be changing into the function, make the variable a global, or make the variable declaration within the function "static".
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
what's the problem with this code in "C"
int& f()
{
int x;
return x;
}
Code:int& f() /* Returns a reference, invalid C */ { int x; return x; /* Returns a local variable, incorrect C */ }-PreludeCode:/* This is correct, but has a potential bug */ int *f ( void ) { static int x; return &x; }
My best code is written with the delete key.
As already discussed,
int& f()
{
int x;
return x;
}
is incorrect C, because there don't exist references ( int& = reference to int ) in C, only in C++.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.