James Dickens James Dickens - 2 months ago 5
C++ Question

Confusion about pointer to an array as a function parameter

In my textbook about c++ I have the following code example:

using std::cout;
using std::endl;
int main() {
int aArr[4] = { 3,4,2,3 };
int bArr[3] = { 2,3,1 };

cout << "Append: " << endl;
printArray(aArr, 4); cout << " + "; printArray(bArr, 3);
int* cArr = append(&aArr, bArr);
cout << " = "; printArray(cArr, 7); cout << endl;
return 0;


}


Does the "&" symbol in front of "aArr" in the call to append in main mean that the address of aArr is passed, or that a reference to aArr is passed.

The question then asks for me to implement a function append which takes two arrays: the first array (in the first argument) of size 4 by array pointer and the second array (in the second argument) of size 3 by reference and returns a pointer to an array of size 7. I have declared that function as (in the appropriate header file)

int* append( int foo[4], int (&secondArray) [3] );


Has the author perhaps misplaced the order of the "&" symbol in the append method (that it should be in front of "bArr")?

Answer

The compiler can help you out in cases like this.

Lets assume that this is the function prototype for your append function:

int* append( int foo[4], int (&secondArray) [3]);

I can test this out with this simple bit of code:

int* append( int foo[4], int (&secondArray) [3])
{
    return 0;
}

int main() {
    int aArr[4] = { 3,4,2,3 };
    int bArr[3] = { 2,3,1 };
    int* cArr = append(&aArr, bArr);
    return 0;
}

But the compiler doesn't like this, failing with this error:

test.cpp(9): error C2664: 'int *append(int [],int (&)[3])': 
          cannot convert argument 1 from 'int (*)[4]' to 'int []'

As you can see it doesn't like the &aArr argument 1 at line 9 as it does not match the argument 1 defined by the function at line 1. From the error message it is even nice enough to give a reason why it thinks they don't line up.

Now using the hint from the compiler it is clear the function should in fact look like this:

int *append(int (*foo)[4], int secondArray[3])
{
    return 0;
}

int main() {
    int aArr[4] = { 3,4,2,3 };
    int bArr[3] = { 2,3,1 };
    int* cArr = append(&aArr, bArr);
    return 0;
}

With that change the compiler is happy to accept the code as correct.

Now comparing the two you can see the difference is in the first case the first argument was passed as an array of 4 integers, whereas in the second case it is passed as the address of an array of four integers.

Just from the english you can tell these are two very different things.

EDIT: Here is an extension of that example that shows how to access the data inside the function.

#include <stdio.h>

int *append(int (*foo)[4], int secondArray[3] )
{
    int *foo1 = *foo;

    for (int i = 0; i < 4; ++i)
    {
        printf("foo: %d\n", foo1[i]);
    }
    for (int j = 0; j < 3; ++j)
    {
        printf("secondArray: %d\n", secondArray[j]);
    }
    return 0;
}

int main() {
    int aArr[4] = { 3,4,2,3 };
    int bArr[3] = { 12,13,11 };
    int* cArr = append(&aArr, bArr);
    return 0;
}

Compiling an running this code produces this output:

foo: 3
foo: 4
foo: 2
foo: 3
secondArray: 12
secondArray: 13
secondArray: 11
Comments