Sign in to follow this  

simple variable question

This topic is 3463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Question about variables getting lost. Is it possible. void f(int &value) { //value 0(missing at this point) } int main(void) { int val = 10; if(val) f(10); return 0; }

Share this post


Link to post
Share on other sites
Passing an integer literal to f() isn't even possible the way it is defined in your snippet. You'd have to change the signature of f() to

void f(const int& value);


Apart from that, I don't really understand your question. What variable exactly is getting 'lost'?

Share this post


Link to post
Share on other sites
Forgive me if i'm wrong, but i'm working on the assumption that this is C/C++ code. The & symbol in front of value is the address of or reference to operator, which is going to say address of the integer value 10, you should be able to just use int value. so when you try to use the variable 'value' it's not really there, it's jus Also, i'm not sure what the code here is to accomplish, some context may help.

For example this segment of code:


int main(void)
{

int val = 10;

//this if statement seems entirely useless
if(val) f(10);

return 0;
}


the if statement here will always process f(10) because you've set val = 10, i imagine you're trying to do something more like


if (val == f(10))
{
//do stuff
}



Again, forgive me if i'm wrong, a little more information would be greatly useful here though. From your code it looks like you want to pass it the value 10, not a reference to the value.

Share this post


Link to post
Share on other sites

This topic is 3463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this