c - Pointers and multidimensional arrays -
i want have array of k
2-element arrays of ints. code:
int **pipe_fd_ptr; pipe_fd_ptr = malloc(k*sizeof(int*)); for(i = 0; < k; i++) { pipe_fd_ptr = malloc(2*sizeof(int)); } // testing for(i = 0; i<k; i++) { for(j=0;j<2;j++) pipe_fd_ptr[i][j] =j; }
i segfault. doing wrong?
should
for(i = 0; < k; i++) { pipe_fd_ptr[ ] = malloc(2*sizeof(int)); }
with pipe_fd_ptr = malloc(k*sizeof(int*));
allocated space k pointers int iterate trough array , allocate memory each pointer pipe_fd_ptr[ ] = malloc(2*sizeof(int));
Comments
Post a Comment