7

How should the try() function be modified (and it's call) to get the output as 11 from the program below?

#include <stdio.h> /* declare try() here */ int main(void) { int x = 10; try(); /* Call can change */ printf("%d\n", x); return 0; } void try() /* Signature can change */ { /* how to change x here?? */ } 
0

3 Answers 3

20

To change the value of x from within a function, have try() take a pointer to the variable and change it there.

e.g.,

void try(int *x) { *x = 11; } int main() { int x = 10; try(&x); printf("%d",x); return 0; } 
Sign up to request clarification or add additional context in comments.

9 Comments

Don't use pointers unless you have good reason to. Pass-by-reference does the job just as well.
This is C code, not C++. References are not available.
It was originally tagged as C so we shouldn't assume it is C++.
@Computer Guru, Jeff M: Honestly, I agree with both of you. Although it can be confusing to a beginner, I think it is better to give more information rather than less.
If this WAS C++, it wouldn't compile anyhow. He named a function "try". That's a reserved word. Read the tags.
|
4

The other answers are correct. The only way to truly change a variable inside another function is to pass it via pointer. Jeff M's example is the best, here.

If it doesn't really have to be that exact same variable, you can return the value from that function, and re-assign it to the variable, ala:

int try(int x) { x = x + 1; return x; } int main() { int x = 10; x = try(x); printf("%d",x); return 0; } 

Another option is to make it global (but don't do this very often - it is extremely messy!):

int x; void try() { x = 5; } int main() { x = 10; try(); printf("%d",x); return 0; } 

4 Comments

Please note - since we're talking beginner tag, here - that try() needs to return x = x + 1; in the first example...
@djacobson: As in, since he's a beginner, my example should be correct? :) OK, fixed.
Personally I think there should probably not even be an assignment within that function. It alters only its argument, and assignments as expressions is a likely beginner pitfall. Notably that assignment has no effect on x in main(), only the return followed by handling the returned value does.
@Yann Vernier: Sometimes there are good reasons to write functions like this example, though I understand if it would be confusing to a new programmer. If you're talking about what this example program could be, This whole program could be boiled down to a single line, with no variables: printf(%d", 5); Also, learning pointers is generally a huge sore point for new C programmers.
1

You need to pass a pointer to the memory location (a copy of the original pointer). Otherwise you are just modifying a copy of the original value which is gone when the function exits.

void Try( int *x ); int main( void ) { int x = 10; Try( &x ); /* ... */ } void Try( int *x ) { *x = 11; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.