C Selection of two-dimensional arrays

I am trying to allocate an array of two-dimensional dimensions of file descriptors ... So I need something like this FD [0] [0] FD [0] [1]

I have encoded so far:

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));
    }
  }

How can I set everything to all zeros right now, I keep getting a seg error when I try to assign a value ...

thank

+3
source share
2 answers

short answer: change your inner malloc to calloc.

long answer provided by c faq: http://c-faq.com/~scs/cclass/int/sx9b.html

, , C . . , .

+2

, myPipes:

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

:

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

, , :

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

Source: https://habr.com/ru/post/1740084/


All Articles