simple variable question

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

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 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 on other sites
Yes, you can pass parameters to functions and never use them.

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 uselessif(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.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 30
• 12
• 12
• 11
• Forum Statistics

• Total Topics
631408
• Total Posts
2999922
×