如何从函数返回矩阵(二维数组)?(C) [英] How to return matrix (2D array) from function? (C)

查看:40
本文介绍了如何从函数返回矩阵(二维数组)?(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个生成宾果板的函数,我想返回宾果板.

正如我没想到的那样,它不起作用.

这里是功能:

int** generateBoard() {int board[N]​​[M], i, j , fillNum;布尔存在 = 真;//初始化种子srand(时间(空));//填上..for(i = 0; i 

我在返回板"行出现编译错误(红色子线).

有没有办法在不使用结构动态分配的情况下返回一个二维数组?

我使用的是 Microsoft Visual C++ Express 2010.

解决方案

必须有人某处拥有那个板子的内存,更重要的是,所有权必须延伸回这个函数的调用者.如果没有动态分配,您唯一的其他真正选择是将其作为输入/输出参数发送到函数中.

void generateBoard(size_t N, size_t M, int board[N]​​[M]){int i, j , fillNum;布尔存在 = 真;//初始化种子srand(时间(空));//填上..for(i = 0; i 

并从您的调用者处像这样调用:

int main(){const size_t N = 10;const size_t M = 10;国际板[N][M];生成板(N,M,板);...}

我还会考虑将 srand() 调用重新定位到 main() 中的启动代码.理想情况下,它应该永远不会出现在某些潜在的可重复调用的函数中,并且应该保证每次流程执行仅执行一次.(注意:老实说,我不记得是否每个 线程 执行一次,但在您的编码学习曲线的这一点上,我猜多线程还没有出现).

最后,您的随机填充循环是不必要的重复.有更好的替代方案可以生成您显然想要做的事情:创建一组现有数字的随机排列.正如所写的那样,您可以旋转一段时间来尝试填充最后几个插槽,具体取决于 MAX_RANGE(N*M) 相比有多大.

I created a function that generates a bingo board and I want to return the bingo board.

as I didn't expect for , it doesn't work.

here is the function:

int** generateBoard() {
    int board[N][M], i, j , fillNum;
    Boolean exists = True;
    // initilize seed
    srand(time(NULL));
    // fill up..
    for(i = 0; i < N; ++i) {
        for(j = 0; j < M; ++j) {
            exists = True;
            while(exists) {
                fillNum = rand()%MAX_RANGE + 1; // limit up to MAX_RANGE
                if(beenAdded(board, fillNum) == Exist) {
                    continue;
                } else {
                    board[i][j] = fillNum;
                    exists = False;
                }
            }
        }
    }
    return board;
}

I have a compilcation error (the red subline) at "return board" line.

is there way to return a 2D array without using structs dynamic allocations?

I'm using Microsoft Visual C++ Express 2010.

解决方案

Someone has to own the memory of that board somewhere, and more importantly, that ownership must extend back to the caller of this function. Without dynamic allocation, your only other real alternative is to send it into the function as in in/out parameter.

void generateBoard(size_t N, size_t M, int board[N][M])
{
    int i, j , fillNum;
    Boolean exists = True;
    // initilize seed
    srand(time(NULL));
    // fill up..
    for(i = 0; i < N; ++i) {
        for(j = 0; j < M; ++j) {
            exists = True;
            while(exists) {
                fillNum = rand()%MAX_RANGE + 1; // limit up to MAX_RANGE
                if(beenAdded(board, fillNum) == Exist) {
                    continue;
                } else {
                    board[i][j] = fillNum;
                    exists = False;
                }
            }
        }
    }
}

and invoke like this from your caller:

int main()
{
    const size_t N = 10;
    const size_t M = 10;
    int board[N][M];

    generateBoard(N,M,board);

    ...
}

I would also consider relocatting the srand() call to the startup code in main(). It should ideally never be in some potentially repeat-callable function, and should be guaranteed to only be executed once per process execution. (note: I honestly can't remember if it is once per thread execution, but at this point in your coding learning curve I'm guessing multi-threading is not on the radar yet).

Finally, your random-fill loop is needlessly repetitious. There are better alternatives generating what you're apparently trying to do: create a random permutation of an existing set of numbers. As written you could spin for some time trying to fill those last few slots, depending on how much larger MAX_RANGE is compared to (N*M).

这篇关于如何从函数返回矩阵(二维数组)?(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆