我正在尝试分配文件描述符的二维数组...所以我需要这样的东西 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/