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;
}
Printable View
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
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".
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;
}
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++.