سؤال

Why is there a difference in the results when I call an array inside a void function and when I call a scalar inside a void function:

Pass an array into a void function:

#include <iostream>
const int n_cells = 1;
using namespace std;

void function1(int c[n_cells], int a, int b)
{
    c[0] = a + b;
}

int main(){
    int a = 3;
    int b = 4;
    int c[n_cells];
    function1(c, a, b);
    cout<<"c = "<<c[0];

    return 1;
}

Result:

c = 7

Pass a scalar into a void function

#include <iostream>

using namespace std;

void function1(int c, int a, int b)
{
    c = a + b;
}

int main(){
    int a = 3;
    int b = 4;
    int c;
    function1(c, a, b);
    cout<<"c = "<<c;

    return 1;
}

Result:

c = 2130567168 //Some trash value

P.S. Any comments on why I receive the same trash value as given above every single time?

هل كانت مفيدة؟

المحلول

void function1(int c[n_cells], int a, int b)

effectively passes a pointer to the caller's array. function1 then operates on the caller's array meaning that any updates are available to the caller.

void function1(int c, int a, int b)

passes a copy of c. It does not have access to the caller's variable so cannot update it. main never assigned c so you print out an uninitialised value.

If you want to update an integer argument, you can pass it by reference instead

void function1(int& c, int a, int b)
//                ^

Rather than passing a copy of the caller's c, this now passes a pointer to the caller's variable, allowing function1 to update it.

نصائح أخرى

The array parameter is actually transformed to the type int*, so what you're actually doing is passing a pointer to the first element of the array declared in main. So when you assign to the first element of this array, you are modifying the array in main.

However, when you pass an int, the int is copied into the function and you modify that copy. This modification will not be seen in main.

You could get the same result in the second program if you would define the function the following way

void function1(int *c, int a, int b)
{
    c[0] = a + b;
}

When you pass an array by value it is converted implicitly by the compiler to pointer to its first element. So these function declarations are equivalent and declare the same function

void function1(int c[n_cells], int a, int b);
void function1(int c[10], int a, int b);
void function1(int c[], int a, int b);
void function1(int *c, int a, int b);

Compare the last declaration with the declaration I showed you for the second program.

In the second program the function gets a copy of its argument. Its parameter is a local variable of the function. So any changes of this local variable will be discarded after exiting the function that is the local variable will be destroyed.

In the first program the function gets the address of the first element of the array. It makes changes at this address. So the corresponding element of the original array will be changed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top