1

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")?

13
  • 1
    &aArr and aArr is the same thing in fact, special case for static arrays. Commented Oct 1, 2016 at 22:05
  • 5
    Read about references. The ambersand means different things in different contexts. In the context of a variable declaration (like argument declarations are) it's not the address-of operator, but means that the variable is a reference. Commented Oct 1, 2016 at 22:05
  • 6
    @PierreEmmanuelLallemant No they are not the same thing in fact! Commented Oct 1, 2016 at 22:07
  • 1
    The author did not misplace anything, and &aArr passes the address of the array (which is a pointer). Your attempt at append expects a the address of the array's first element, so you will need to change it to syntax which expects the address of the whole array. Commented Oct 1, 2016 at 22:18
  • 1
    @PierreEmmanuelLallemant c-faq.com/aryptr/ptrtoarray.html Commented Oct 1, 2016 at 23:10

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ok thanks. Suppose that I want to loop over the elements in foo, how can I do this. This code is not working: for (int i = 0; i < 4; i++) { cout << *foo[i]; }
@JamesDickens (*foo)[i]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.