简体   繁体   English

C ++指针和2d数组输出

[英]C++ Pointer and 2d array outputting

I'm new to C++ and still really confused about how 2d arrays work with pointers. 我是C ++的新手,仍然对2d数组如何与指针一起使用感到困惑。 If I have this (in example format): 如果我有这个(以示例格式):

int* anarray = anarrayfiller();
for (int a=0;a<10;a++) {
    for (int b=0;b<10;b++) {
         cout<<(char)anarray[a][b]; //Here's the error mentioned below
    }
    cout<<"\n";
}
//Later, outside main
int* anarrayfiller() {
    int anarray[10][10];
    //Populated here
    return &anarray;
}

This produces an error under b in the cout<< line: "Expression must have pointer-to-object type" I would just check how to search through 2d arrays, and I found this: A pointer to 2d array Which suggests that actually this pointer points to the array of ints inside anarray[0], and if that's the case, I must be missing something in terms of returning pointers - wouldn't I then have to return a pointer to a 2d array of pointers that each points to a specific int from anarray? 这会在cout <<行中的b下产生一个错误:“表达式必须具有指向对象的指针类型”我只想检查一下如何搜索2d数组,然后发现: 一个指向2d数组的指针,这实际上表明指针指向anarray [0]内的int数组,如果是这种情况,就返回指针而言,我肯定会丢失一些东西-然后我就不必将指针返回至每个指向的二维指针数组了来自anarray的特定int? I'm pretty confused here. 我在这里很困惑。 How do pointers work with 2d arrays? 指针如何与2D数组一起使用?

You have a few errors here: 您在这里有一些错误:

  1. You return a pointer to a local variable. 您返回一个指向局部变量的指针。 After the function returns the stack area previously occupied by that variable no longer exist, or is used by the next function. 函数返回后,先前由该变量占用的堆栈区域不再存在,或由下一个函数使用。

  2. You return a pointer to an integer, while you have a two-dimensional array. 当您具有二维数组时,您将返回一个指向整数的指针。 The closest would be a pointer-to-pointer. 最接近的将是指针到指针。

  3. You access thing single-pointer as though it was a double-pointer (pointer-to-pointer or pointer-to-array or array-or-arrays), but it's not. 您可以访问事物单指针,就好像它是双指针一样(指针到指针或指针到数组或数组或数组),但事实并非如此。 That's the reason you get errors at the pointed to line. 这就是您在指向的行出现错误的原因。

  4. But you can't use pointer to pointer, as the memory layout of an array-of-arrays (a two-dimensional array) is different from a pointer-to-pointer. 但是您不能使用指针指向指针,因为数组数组(二维数组)的内存布局与指针指向指针不同。 See eg this old answer of mine for an explanation of why. 有关原因的解释,请参见例如我的旧答案

This can be solved most easily by creating the array dynamically on the heap, as a pointer-to-pointer: 通过在堆上动态创建数组作为指针到指针,可以最轻松地解决此问题:

int **anarrayfiller()
{
    int **anarray = malloc(sizeof(int *) * 10);

    for (int i = 0; i < 10; ++i)
    {
        anarray[i] = malloc(sizeof(int) * 10);

        /* Populate here */
    }

    return anarray;
}

As you tagged your question as C++, you should actually avoid plain arrays or pointers in favor of either std::vector (if you need to add dynamically) or std::array (if you have a fixed compile-time size): 当您将问题标记为C ++时,实际上应该避免使用纯数组或指针,而推荐使用std::vector (如果需要动态添加)或std::array (如果您具有固定的编译时大小):

std::array<std::array<int, 10>, 10> anarrayfiller()
{
    std::array<std::array<int, 10>, 10> anarray;

    // Populate arrays

    return anarray;
}

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

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