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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -