user2682811
user2682811

Reputation: 195

use value of transfer(by-value?) between function calls with pointers to stackvariables

What is the meaning of "value of transfer"(by-value? Not hundred percent sure on the english term) between function calls. Give me examples of doing this, assume that I use pointers to stackvariables.

I really don't understand the concept of "value of transfer". What should the function return to another function?

If I use pointers like in the example below I just transfer the pointer adress? So how do I use value of transfer with pointer to stackvariables?

void fun1(){
  int x = 44;
  int *y = &x;
}

void fun2(){
  int *y;
  }

From first answer:

   void fun1(){
        int x = 44;
        fun2( &x );
        printf( "%d\n", x );       // prints 55
    }

    void fun2( int *value ){
        printf( "%d\n", *value );  // prints 44
        *value = 55;               // changes the value of `x` in `fun1`

}

For me it seems like I just transfer a pointer to a stack variable(x) to fun2? So the actual question is: How can I use pointers to stack variables for transfering values between function calls?

You probably already answered the question? But I want to be sure on this and wounder if I get it right, so here's what I think so far:I first send a pointer to a stack variable x from fun1 to fun2. And when fun2 is called I update the value of int x = 44 to 55 through *value = 55, and *value is a pointer to a stack variable so I actually updated the value of the variable x with the help of a pointer to a stack variable. But where does I transfer a value between the functions with this technique of pointers to stack variables. Do I transfer a value between the functions? I don't think so, if I do I should have returned something to the other function. For now it only seems like I update a variable between function calls? But maybe the question is already answered? But I am still a little bit troubled about what it means to transfer value between function calls.

Upvotes: 1

Views: 427

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84579

Another way of thinking about pass by value or pass by reference is to understand what is being passed. When you call function (int x), you are passing a copy of x to function. No matter what you do with x in function, the value of x outside of function remains unchanged -- because you just passed a copy of x to function.

On the other hand, when you call function (int *x), you are passing the address of x (a reference to x) to function. That is why & is often referred to as the address of operator. Whatever you do to x in function now changes the memory pointed to by x. So the value of x outside of function is also changed, because you changed the memory pointed to by address &x.

Here is a short annotated example of your functions:

#include <stdio.h>

/* value is copy of variable 'x' holding its value  */
void fun2_by_value ( int value )
{
    printf( "%-17s - %d\n", __func__, value );  /* prints 44    */
    value = 55;   /* values is a copy of x from fun1_by_value   */
}

void fun1_by_value ()
{
    int x = 44;
    fun2_by_value (x);          /* passes copy of 'x's value    */
    printf ( "%-17s - %d\n", __func__, x );     /* prints 44    */
}

/* value is pointer to memory containing 'x' */
void fun2_by_reference ( int *value )
{
    printf ( "%-17s - %d\n", __func__, *value ); /* prints 44   */
    *value = 55;   /* changes mem pointed to by address 'value' */
}

void fun1_by_reference ()
{
    int x = 44;
    fun2_by_reference (&x);         /* passes address of 'x'    */
    printf ( "%-17s - %d\n", __func__, x );     /* prints 55    */
}

int main () {

    printf ("\nCalling 'fun1_by_value ();'\n\n");
    fun1_by_value ();

    printf ("\nCalling 'fun1_by_reference ();'\n\n");
    fun1_by_reference ();

    printf ("\n");

    return 0;
}

output:

$ ./bin/byrbyv

Calling 'fun1_by_value ();'

fun2_by_value     - 44
fun1_by_value     - 44

Calling 'fun1_by_reference ();'

fun2_by_reference - 44
fun1_by_reference - 55

Upvotes: 0

user3386109
user3386109

Reputation: 34839

If you want fun2 to be able to change variable x in fun1, then you pass a pointer-to-x to fun2 like this

// This code demonstrates "pass by address" which (for the C programming
// language) is the same as "pass by reference". 

void fun1(){
    int x = 44;
    fun2( &x );
    printf( "%d\n", x );       // prints 55
}

void fun2( int *value ){
    printf( "%d\n", *value );  // prints 44
    *value = 55;               // changes the value of `x` in `fun1`
}

If you pass x as the parameter instead of the address of x, then fun2 won't be able to change the value of x that fun1 has.

// This code demonstrates "pass by value". fun2 is given the value of x but 
// has no way to change fun1's copy of x.

void fun1( void ){
    int x = 44;
    fun2( x );
    printf( "%d\n", x );      // prints 44
}

void fun2( int value ){
    printf( "%d\n", value );  // prints 44
    value = 55;               // has no effect on `x` in `fun1`
}

Upvotes: 2

Related Questions