0

I have the following C code:

int array_len(int *a) {
    int i;
    for(i = 0; i < ARRAY_SIZE && a[i] != TERMINATOR; i++);
    return i;
}

void jumbleArrays(int *a1, int *a2)
{
    int a1_len = array_len(a1);
    a2 = a1;
    a2[a1_len] = 42;
    if(a1_len <= ARRAY_SIZE)
        a2[a1_len+1] = TERMINATOR;

    a1 = malloc(sizeof(int) * ARRAY_SIZE);
    a1[0] = 41;
    a1[1] = TERMINATOR;
}

int main() 
{
    int a1[ARRAY_SIZE];
    int a2[ARRAY_SIZE];

    a1[0] = 10;
    a1[1] = 4;
    a1[2] = TERMINATOR;

    a2[0] = 7;
    a2[1] = 9;
    a2[2] = 11;
    a2[3] = TERMINATOR;

    jumbleArrays(a1, a2);

    printf("Contents of a1: ");
    for(int i = 0; i < ARRAY_SIZE && a1[i] != TERMINATOR; i++) {
        printf("%d ", a1[i]);
    }
    printf("\n");

    printf("Contents of a2: ");
    for(int i = 0; i < ARRAY_SIZE && a2[i] != TERMINATOR; i++) {
        printf("%d ", a2[i]);
    }
    printf("\n");

    return 0;
}

Why when the program is ran why isn't the output the following?

Contents of a1: 41

Contents of a2: 10 4 42

But instead it is Contents of a1: 10 4 42

Contents of a2: 7 9 11

The JumbleArrays is taking a pointer (nothing but an address) and then makes changes to the existing array a1. It then creates a new array on the heap whose base address will be stored in a1, but it seems like after the jumbleArrays() method returns that address is no longer stored in a1? Overall, I'm not sure as to why a1 and a2 refer to their older arrays when clearly they store different addresses in jumbleArrays.

aDabOfRanch
  • 137
  • 9

1 Answers1

1
void jumbleArrays(int *a1, int *a2)
{
    int a1_len = array_len(a1);
    a2 = a1;

Notice that a1 and a2 are pointers and that after that last line, the jumbleArrays function no longer knows the address of the a2 array in main. Its variable, though called a2 now holds the address of the a1 array. So there is no possible way anything this function does after this line can possibly modify the original a2 array. So your expectation, that it would change the contents of a2 cannot be justified. It doesn't even know where a2 is after this line.

Then we have:

a1 = malloc(sizeof(int) * ARRAY_SIZE);

After this point, the local a1 pointer points to some newly-allocated memory and the original pointer to the original a1 is lost. So nothing after this line can possibly modify the original a1 array -- we no longer know where it is. So your expectation that the subsequent lines will modify the a1 array in main isn't justified.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Well I'm not saying its going to modify the original 2 arrays in main. What I don't get is that isn't the jumbleArrays method changing what each pointer stores, ie what array each pointer refers too. We changed the values that a1 and a2 store so why aren't those changes reflected in main when we print their contents? – aDabOfRanch Apr 09 '19 at 14:30
  • @aDabOfRanch The fact that `main` has a variable named `a1` and jumbleArrays` has a variable also named `a1` doesn't connect those two variables in any magic way. One is an array, the other is a pointer. The pointer can be made to point to whatever `jumbleArrays` wants to make it point to, that has no effect on an array it happened to once point to. – David Schwartz Apr 10 '19 at 21:52