C 分配二维数组

标签 c arrays unix

我正在尝试分配文件描述符的二维数组...所以我需要这样的东西 fd[0][0] fd[0][1]

到目前为止我已经编码了:

void allocateMemory(int row, int col, int ***myPipes){
    int i = 0,i2 = 0;
    myPipes = (int**)malloc(row * sizeof(int*));
    for(i = 0; i < row;i++){
       myPipes[i] = (int*)malloc(col * sizeof(int));
    }
  }

我现在如何将它全部设置为零?当我尝试分配一个值时,我总是遇到段错误...

谢谢

最佳答案

因此,首先,您必须传递一个指向 myPipes 的指针:

void allocateMemory(int rows, int cols, int ***myPipes) { ... }

那么就很简单了:

*myPipes = malloc(sizeof(int) * rows * cols);

当然,您可以这样调用它:

int **somePipes;
allocateMemory(rows, cols, &somePipes);

关于C 分配二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2589108/

相关文章:

c - long long int 在 main() 内部声明

c - 在 C 程序中按空格键期间执行用户输入

C 赋值数组错误

CYGWIN Ms-Dos 路径检测到警告

c++ - 从 'a' 迭代到 'x' 并为每个字母创建一个 '.dat' 文件

c - C 结构类型定义中无用的变量名

javascript - 清理代码的最佳方法是什么?

javascript - 如何在javascript中显示图像数组中的上一张图像

linux - 如何在 unix/Linux 中比较和替换多个文件的整行

c++ - 如何查看库的内部(尤其是 C 标准库)?