Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将二维数组设置为二维返回_C++_Arrays_Multidimensional Array - Fatal编程技术网

C++ 如何将二维数组设置为二维返回

C++ 如何将二维数组设置为二维返回,c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,编辑:很可能找到了解决方案 我想将2d数组设置为2d返回,但它总是给我带来无意义的错误: In function 'int main()': error: expected primary-expression before ']' token error: expected primary-expression before ']' token In function 'int initBoard(int (*)[25])': error: invalid conversion from 'i

编辑:很可能找到了解决方案

我想将2d数组设置为2d返回,但它总是给我带来无意义的错误:

In function 'int main()':
error: expected primary-expression before ']' token
error: expected primary-expression before ']' token
In function 'int initBoard(int (*)[25])':
error: invalid conversion from 'int (*)[25]' to 'int' [-fpermissive]
我不能仅仅找出什么是错误的,以及如何消除错误

#include <iostream>

using namespace std;

const short WIDTH = 80;
const short HEIGHT = 25;

int clearBoard();
int initBoard(int board[WIDTH][HEIGHT]);
int drawBoard();

int main()
{
     int board[WIDTH][HEIGHT] = {{0}};
     board = initBoard(board); // problem is this place AND should be initBoard(board);
     cout << board[79][24]
     return 0;
}

int initBoard(int board[WIDTH][HEIGHT])
{
    unsigned int localWidth  = 1;
    unsigned int localHeight = 1;

    while(localHeight < HEIGHT)
    {
        while(localWidth < WIDTH)
        {
            board[localWidth][localHeight] = 0;
            localWidth++;
        }
        localHeight++;
        localWidth = 1;
    }
}

数组总是通过引用传递的,您不需要显式返回它。在函数中对其所做的任何更改都是全局性的。出现编译错误是因为2d数组在技术上是指向int或int**的指针。您可以将initBoard更改为void,它应该可以工作。您不分配数组,而是声明它们


您可以执行动态分配,而不是编译时分配,但由于您似乎知道数组的大小,因此这可能比实际情况更麻烦。

函数initBoard的返回类型为int:

int initBoard(int board[WIDTH][HEIGHT]);
您正试图在return语句中的函数体内部转换为int type int*[HEIGHT]类型,并在调用函数的语句中将int类型的对象分配给array

在C/C++中,数组没有赋值运算符

用以下方法定义函数就足够了

void initBoard(int board[WIDTH][HEIGHT])
{
    unsigned int localWidth  = 1;
    unsigned int localHeight = 1;

    while(localHeight < HEIGHT)
    {
        while(localWidth < WIDTH)
        {
            board[localWidth][localHeight] = 0;
            localWidth++;
        }
        localHeight++;
        localWidth = 1;
    }
}

此外,函数被声明为返回int,但OP试图将数组指针返回为int。在此上下文中,函数应返回void和返回board;声明应该删除。哦,亲爱的;哦,天哪!不,数组不是int*。我对你最初的答案投了更高的票,因为它是准确的,尽管有点不足。我现在投了反对票,因为这是错误的,但如果你纠正它,我将取消我的反对票。在许多上下文中,数组的名称会转换为int*。但是,在这个上下文中,数组是一个2D数组,它变成了int *[25 ],指向25 int数组的指针。此外,你不能在C++或C中分配数组。你已经修改了它,但是2D数组不同于int。类型的数组和类型的指针之间存在差异。在某些上下文中,这两者看起来相似,因为数组会自动转换为指向该类型的指针,但仍然存在差异。根据是否有int矩阵[10][20],查找值矩阵[i][j]的机制有很大的不同;或int**矩阵;或者,实际上是int*矩阵[10];作为变量的定义,这很公平。我会读一些关于这个主题的书。欢迎来到堆栈溢出。请尽快阅读这一页。您确定粘贴的代码会生成关于]的两个错误吗?不应该;当我编译代码时,它不起作用。它合理地抱怨其他事情,但它并不抱怨。您可能需要在程序上运行预处理器并查看输出,这可能会让人望而生畏。我从您的前17行代码中获得了16437行输出;幸运的是,我只需要查看最后17行输出。如果您使用的是g++,请尝试g++-E program.cpp并查看数组定义是否正确。
initBoard(board);