5

I know when we pass a parameter to the function a copy is created in the function's stack and there is no change in the actual value of the parameter in the caller's function.

What happens when you pass a pointer to the function? I know the value of the parameter does get changed in the callers function. But how does it happen internally? How does the called function access a variable in the caller's function?

I tried to get information from Call_by_reference page in wikipeidia but was not substantial.

I am confused with this once I started to read about strings and passing strings as parameters to other functions. Any help regarding this would be great help. Thanks!!!!

Kozet
  • 1,103
  • 1
  • 11
  • 19
Shash
  • 4,160
  • 8
  • 43
  • 67

5 Answers5

10

When you pass a pointer to a function, the pointer gets copied. However, a copy of a pointer to an object x is also a pointer to x, so it can be used to modify x.

For a (contrived) analogy, suppose that x is your house. By the rules of C, when you need a plumber to fix something in your house, you can either pass the plumber a copy of your house, have them fix that, and give the copy back to you. Needless to say, for houses larger than a few bytes, that's quite inefficient due to all the copying. So instead, you give the plumber a pointer to your house (its address), so that the plumber can access your house and fix it on the spot. That's what call-by-reference is: you pass not the data you want modified, but a pointer to that data, so that the callee knows at which location to operate instead of only on which value.

const int broken = 0, fixed = 1;

struct House {
    int plumbing;
};

void plumber(House *h)
{
    h->plumbing = fixed;
}

int main()
{
    struct House h;
    h.plumbing = broken;
    plumber(&h);          // give the plumber the address of the house,
                          // not a copy of the house
    assert(h.plumbing == fixed);
}

In the case of passing strings, what you pass is a pointer to the first char in the string. With pointer arithmetic, you can then get to the following elements.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
4

C doesn't have pass by reference. When you pass a pointer to a function, you are actually passing the address of a variable to the function. The function can then change the values at that address and this change will then reflect in the variable in calling function. However, if you try to change the address that the pointer points to, the changes won't be reflected in the calling function as pointer was still passed by value.

void f(int *j) {
  (*j)++;
  int k = 20;
  j = &k;
}

int main() {
  int i = 20;
  int *p = &i;
  printf("i = %d, p = %p\n", i, p);
  f(p);
  printf("i = %d, p = %p\n", i, p);

  return 0;
}

Output

i = 20, p = 0x123456
i = 21, p = 0x123456
Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43
4

Every parameter in C is passed by value.

jacekmigacz
  • 789
  • 12
  • 22
3

All data is stored in a particular location with a particular address. When you pass by reference, you pass the address of the data by reference. And tell the function to manipulate the data using indirect addressing.

When you pass it to a function, the address(of a variable) is sent as data, but the data itself is pass by value.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
3
                        a=10     b=20
                        ------   -------
                        0xf8      0Xf4
                    ----------  ---------

                Suppose a and b are two variables in a function stack
                when you call another function 
                foo(&a,&b)
                actually 0xf8 and 0xf4 is caught in the definition of 
                foo(int *pa,int *pb)

       So  pa=0xf8    pb=0xf4
           --------    ---------
 But *pa = 10 and *pb = 20 , You can manipulate like this.
 So these pointers start pointing to the actual data and any changes here are reflected in the original environment.
Omkant
  • 9,018
  • 8
  • 39
  • 59