简体   繁体   中英

Why isn't the array being passed by reference?

Why is it that x prints out 1,2,3,4 instead of 4,3,2,1. When i print input[0] it says 4. Shouldn't the array have been passed by reference? So I can use x to refer to input which was originally x? This is C++ by the way. This is not a duplicate of the other question. If I print out the array in rev listo, it prints correctly. The issue is in terms of the array being callable in the main function.

using namespace std;

void rev_listo(int input[], int num)
{
    int end_list[num];

    for (int x = 0; x<num; x++) {
        end_list[x]=input[num-x-1];
        // cout<<end_list[x]<<endl;
    }
    // std::cout<<input<<std::endl;

    input=end_list;
    cout<<input[0]<<endl;
}

int main() {
    int x [4];
    x[0]=1;
    x[1]=2;
    x[2]=3;
    x[3]=4;

    rev_listo(x,  4);

    for(int y = 0; y<4; y++) {
        std::cout<<x[y]<<std::endl;
    }

    return 0;
 }

Internally, When you do void rev_listo(int input[], int num) , a pointer pointing to first element of the array is passed (just to avoid copying of array).

Like this:

void rev_listo(int *input /* = &(arr[0])*/, int num)

Please note that the input pointer itself is copied by value, not by reference. So, When you do this, address of first element of end_list is copied to input pointer.:

input=end_list;

When the function ends, the pointer input dies.

void rev_listo(int input[], int num)
{

    int end_list[num];

    for(int x = 0; x<num; x++){
       end_list[x]=input[num-x-1];
    //cout<<end_list[x]<<endl;
    }
   // std::cout<<input<<std::endl;

    input=end_list;
   cout<<input[0]<<endl;
} // input pointer dies here. So does num.

Instead of just assigning the pointer like this: input=end_list; , you should copy the array manually. Like this:

for(int x = 0; x<num; x++){
   input[x] = end_list[x];
}

Shouldn't the array have been passed by reference?

No. An argument will be a reference only if the type of the argument is a reference type. A reference to type T is declared like this: T& . You'll find that there is no & in your argument declaration. You've declared the argument to be a pointer to an integer.

Although the argument declaration looks like an array (with the size missing), it isn't actually an array. Arguments cannot have an array type. Arguments that are declared to be arrays are adjusted to be a pointer to an element of such array. Your function declaration is equivalent to void rev_listo(int *input, int num) .

Why is it that x prints out 1,2,3,4

Because the function doesn't modify the array elements pointed by the argument input . It only modifies a local array end_list , and the local pointer input .


PS The declaration int end_list[num]; is ill-formed in C++ because num is not a compile time constant.

To avoid rewriting on the legitimately occupied memory locations; iterate over only half of the size. Eg

void revvings(int *d, int size) {
    int dh = (int)size/2;
       for(int i=0; i<dh; ++i) {
       int df = d[i];
       int dl = d[size-1-i];
       d[i]        = dl;
       d[size-1-i] = df;
    }
}

#include <cstdio>

int main() {
    int size = 7;
    int *d = new int[size];
    d[0] = 0; d[1] = 1; d[2] = 2; d[3] = 3; d[4] = 4; d[5] = 5; d[6] = 6;
    for(int i=0; i<size; ++i) printf("{%d} ", d[i]);
    puts("");    
    revvings(d, size);
    for(int i=0; i<size; ++i) printf("{%d} ", d[i]);
    puts("");




    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM