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?? */
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
kaka
  • 79
  • 1
  • 1
  • 3

3 Answers3

21

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 Mercado
  • 129,526
  • 32
  • 251
  • 272
  • 1
    Don't use pointers unless you have good reason to. Pass-by-reference does the job just as well. – Mahmoud Al-Qudsi Jul 31 '10 at 06:58
  • 26
    This is C code, not C++. References are not available. – Jeff Mercado Jul 31 '10 at 06:59
  • @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. – Mahmoud Al-Qudsi Jul 31 '10 at 07:00
  • 4
    It was originally tagged as `C` so we shouldn't assume it is C++. – Jeff Mercado Jul 31 '10 at 07:02
  • 2
    @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 Jul 31 '10 at 07:03
  • 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 Jul 31 '10 at 08:27
  • @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 Mercado Jul 31 '10 at 08:36
  • 1
    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.. GitHub STOP HELPING ICE Jul 31 '10 at 15:36
  • 5
    If this WAS C++, it wouldn't compile anyhow. He named a function "try". That's a reserved word. Read the tags. – Clark Gaebel Jul 31 '10 at 23:03
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;
}
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • Please note - since we're talking beginner tag, here - that try() needs to `return x = x + 1;` in the first example... – Dan J Jul 31 '10 at 07:06
  • @djacobson: As in, since he's a beginner, my example should be correct? :) OK, fixed. – Merlyn Morgan-Graham Jul 31 '10 at 07:19
  • 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 Jul 31 '10 at 08:31
  • @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 Jul 31 '10 at 09:45
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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ed S.
  • 122,712
  • 22
  • 185
  • 265