Sunday, April 27, 2008

Pass by value/address

If the declaration of argument matches the declaration of the formal parameter, then the argument is passed by value.

void f (char * a)
{
   a++;
}
int main (void)
{
  char *a = "abc";
  f(a);
  puts (a);
  return 0;
}

It's pass by value.  You are passing the value of the pointer a. So you get 'abc' instead of 'bc'. If you want to get 'bc', use: void f (char * &a)

No comments:

Post a Comment