Search code examples
carrayspass-by-referenceimplicit-conversionfunction-declaration

C pass array by value vs pass array by reference


Hello everyone so I have been learning C and I came across this example and I don't really find the logic behind. this is not a technical question regarding code I was looking more for an explanation. this is the code

#include <stdio.h>

void set_array(int array[4]);
void set_int(int x);

int main(void)
{
    int a = 10;
    int b[4] = {0, 1, 2, 3};
    set_int(a);
    set_array(b);
    printf("%d %d\n", a, b[0]);
}

void set_array(int array[4])
{
    array[0] = 22;
}

void  set_int(int x)
{
    x = 22;
}

the output is 10 22

basically my question is why does the 22 that is passed from the set_array function rewrites the 0 on the b array and actually prints but the 22 that is passed from the set_int function doesn't get passed nor printed.

Thank you all!


Solution

  • In C passing by reference means passing an object indirectly through a pointer to it. If you will pass an object directly to a function then the function will deal with a copy of the value of the object.

    Compare two function calls in this demonstrative program.

    #include <stdio.h>
    
    void f( int x )
    {
        x = 10;
    }
    
    void g( int *x )
    {
        *x = 10;
    }
    
    
    int main(void) 
    {
        int x = 0;
    
        printf( "Before call f: x = %d\n", x );
    
        f( x );
    
        printf( "After  call f: x = %d\n", x );
    
        putchar( '\n' );
    
        printf( "Before call g: x = %d\n", x );
    
        g( &x );
    
        printf( "After  call g: x = %d\n", x );
    
        return 0;
    }
    

    The program output is

    Before call f: x = 0
    After  call f: x = 0
    
    Before call g: x = 0
    After  call g: x = 10
    

    That is we passed to the function f the object x directly. So the function deals with a copy of the object x. Changing the copy does not influence on the original object x declared in main.

    As for the function g then it got an access to the object x indirectly through a pointer to it. So changing the pointed object we changed the object x declared in main.

    As for arrays then when an array is passed to a function it is implicitly converted to pointer to its first element. So the function in fact gets elements of the array by reference through this pointer. Using the pointer and the pointer arithmetic we can change any element of the array.

    This function declaration

    void set_array(int array[4]);
    

    is equivalent to the following declaration

    void set_array(int array[]);
    

    and the both declarations are adjusted by the compiler to the declaration

    void set_array(int *array);
    

    So the function deals with a pointer - the pointer to the first element of the array used as an argument of the function call.

    The call of the function set_array in your program

    set_array(b);
    

    is equivalent to the following call

    set_array( &b[0] );
    

    because arrays used in expressions as for example as a function argument expression are implicitly converted to pointers to their first elements.