0

Can anyone point out why I am getting an error in this code and what should be the fix? This did seem to work for others. Thank you!

I am trying to code an example for pass by reference for arrays

#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;
}
Parker
  • 351
  • 2
  • 9
Pallavi
  • 9
  • 2
  • 3
    What error are you getting? – wallyk May 28 '23 at 03:09
  • 2
    A statement such as "it's not working" is not a sufficient description of the problem. Please elaborate, for example by specifying the desired output and the actual output. – Andreas Wenzel May 28 '23 at 03:30
  • The literal answer to *"Why is pass by reference not working in C?"* is that there *is* no pass by reference in C. There is only pass by value. – BoP May 28 '23 at 10:42

2 Answers2

4
x = 22;
}

Why this is not working?

The code you provided is not working as intended because you are passing the arguments to the functions set_array and set_int by value instead of by reference. In C, function arguments are passed by value, which means that a copy of the argument is made and any modifications made to the argument within the function will not affect the original variable.

In the set_array function, when you modify array[0], it will affect the original array because arrays in C are passed by reference (the array name acts as a pointer to the first element). However, in the set_int function, you are modifying the local variable x, which is a copy of the a variable in the main function. Therefore, the modification does not affect the original a variable.

To fix this issue, you can modify the functions to accept pointers to the variables instead. Here's an updated version of your code that demonstrates the correct usage of pointers:

#include <stdio.h>

void set_array(int* array);
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)
{
   array[0] = 22;
}

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


Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 4
    It was not necessary (but also not wrong) to change the parameter declaration `int array[4]` to `int* array`, because `int array[4]` will [decay](https://stackoverflow.com/q/1461432/12149471) to a pointer. Therefore, both parameter declarations are equivalent. – Andreas Wenzel May 28 '23 at 03:41
1

When you pass an argument to a function you are actually passing a copy of the value. So when you do

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

int main(int argc, char** argv) {
    int a = 20;
    set_int(22);
    printf("%d\n", a);
}

What you are essentially doing is copying the value of a and putting it into x, now you have to variables, a and x. x is set to 22 and you return out of the function, which C will remove x from memory automatically.

To get the correct variable you can use pointers which are an integer (technically unsigned long but int for our purposes) value that represent a point in memory where another value is stored.

int main(int arc, char** argv) {
    int a = 20;
    int* b = &a;
}

In this example b is of type "int pointer" which points to a. We can update the set_int() function to "dereference" our new pointer value.

void set_int(int* x) {
    // Dereference out int* with the * operator
    // y is out original integer a at the same spot in memory
    int y = *x;
    // Because we are using a we can now set it to out value of 22
    y = 22;
}

int main(int arc, char** argv) {
    int a = 20;
    int* b = &a;
    set_int(b);
    // This should print 22
    printf("%d\n", a);
    return 0;
}

Note that set_int() is written this way to help understand how pointers work, normally you would compact this into one line:

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

And of course we can also compact our call:

int main(int argc, char** argv) {
    int a = 20;
    set_int(&a);
    printf("%d\n", a);
    return 0;
}