c++和c动态申请二维数组

这是我面试中遇到的一道题,用c和c++分别申请一个二维数组,int **res,要求申请后的可以使用res[3][4]这一类防存方式。

这个是没有错误检查的版本。

答案:

c++语言的版本

int **allocate(int row, int column)
{
    int **res = new int*[row];
    for (int i = 0; i < row; i++) {
        res[i] = new int[column];
    }
    return res;
}

c语言

int **allocate(int  row, int column)
{
    int **res = (int **)malloc(sizeof(int *) * row);
    for (int i = 0; i < row; i++) {
        res[i] = (int *)malloc(sizeof(int) * column);
    }
    return res;
}
原文地址:https://www.cnblogs.com/jfwang/p/4296179.html