简体   繁体   English

关于将数组指针作为函数参数的困惑

[英]Confusion about pointer to an array as a function parameter

In my textbook about c++ I have the following code example:在我关于 c++ 的教科书中,我有以下代码示例:

  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.调用 append in main 中“aArr”前面的“&”符号是否表示传递了 aArr 的地址,或者传递了对 aArr 的引用。

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)然后这个问题要求我实现一个函数 append,它接受两个数组:第一个数组(在第一个参数中)通过数组指针大小为 4,第二个数组(在第二个参数中)通过引用大小为 3 并返回一个指针到大小为 7 的数组。我已将该函数声明为(在适当的头文件中)

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")?作者是否可能在 append 方法中放错了“&”符号的顺序(它应该在“bArr”之前)?

The compiler can help you out in cases like this.在这种情况下,编译器可以帮助您。

Lets assume that this is the function prototype for your append function:让我们假设这是您的 append 函数的函数原型:

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.正如您所看到的,它不喜欢第 9 行的&aArr参数 1,因为它与第 1 行的函数定义的参数 1 不匹配。从错误消息中,它甚至足以说明为什么它认为它们不匹配不排队。

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.现在比较两者,您可以看到不同之处在于,在第一种情况下,第一个参数作为 4 个整数数组传递,而在第二种情况下,它作为 4 个整数数组的地址传递。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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