tags:

views:

153

answers:

3

what should try() function should have to get the output as 11 from the below program?

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

try()
{
    /* how to change x here?? */
}
A: 

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( ... )
{
    int x = 10;
    Try( &x );
    /* ... */
}

void Try( int *x )
{
    *x = 11;
}
Ed Swangren
yeah, but it needs to be.
Ed Swangren
@Ronny: It doesn't need to be a pointer to pass it as one.The address of any variable in c++ is a pointer to that same variable.
Computer Guru
@Ronny: Agreeing with Computer Guru. You can get a pointer to any variable by using the " int* pX = *pX = 5; Now x will equal 5.
Merlyn Morgan-Graham
@Yann: Ummm... I think you are missing something. You do know that int *x is a pointer to int, right? int* x and int *x are the same thing, and the latter follows the "declaration like use" idiom of C. (ok, I wish I knew how to escape an asterisk to avoid the italics, but you are wrong Yann)
Ed Swangren
Formatting error, it seems; at least when I'm reading this, it looks like the asterisk is missing on the first line, but present at the definition (4th line from the end). It ought to be there for both.
Yann Vernier
Ahh, ok, sorry. Fixed it.
Ed Swangren
Glad to see the confusion cleared up. By the way, I think backticks can be used to write inline code such as `int*` or `*pX`.
Yann Vernier
+6  A: 

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;
}
Jeff M
Don't use pointers unless you have good reason to. Pass-by-reference does the job just as well.
Computer Guru
This is C code, not C++. References are not available.
Jeff M
@Jeff: Good point. Is it though? As a beginner question, he could just be mixing c and c++ up, ant not really have any particular preference in the matter.
Computer Guru
It was originally tagged as `C` so we shouldn't assume it is C++.
Jeff M
@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.
Merlyn Morgan-Graham
IMHO, pointers have an advantage in that they hint the variable may be modified where the call is made. Technically references work precisely the same, you just get less indication of the variable's scope within the code itself. Pointer arithmetic is another story.
Yann Vernier
@Yann: The best way to do that is to apply const-correctness along with well-behaved code. That would be the best indicator I'd think.
Jeff M
References are an abomination (call syntax hides the fact that the called function could modify the variable) and should never be used except where needed in operator overloading, etc..
R..
If this WAS C++, it wouldn't compile anyhow. He named a function "try". That's a reserved word. Read the tags.
Clark Gaebel
+3  A: 

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;
}
Merlyn Morgan-Graham
Please note - since we're talking beginner tag, here - that try() needs to `return x = x + 1;` in the first example...
djacobson
@djacobson: As in, since he's a beginner, my example should be correct? :) OK, fixed.
Merlyn Morgan-Graham
+1 for providing many options for the solution.
Praveen S
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
@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.
Merlyn Morgan-Graham