C Programming: malloc() for a 2D array (using pointer-to-pointer)

前端 未结 4 1182
抹茶落季
抹茶落季 2020-12-09 23:02

yesterday I had posted a question: How should I pass a pointer to a function and allocate memory for the passed pointer from inside the called function?

From the ans

4条回答
  •  情歌与酒
    2020-12-09 23:39

    In your alloc_2D_pixels function, you need another level of indirection when accessing memory. As it is now, you only modify the parameter, not the pointer pointed to by the parameter. For example,

    memory = malloc(rows * sizeof(unsigned char** ));
    // becomes
    *memory = malloc(rows * sizeof(unsigned char** ));
    
    // and later...
    memory[i] = malloc(cols * sizeof(unsigned char));
    // becomes
    (*memory)[i] = malloc(cols * sizeof(unsigned char));
    

    (basically, anywhere you are using memory, you need to use (*memory); the parentheses are only needed when you are using subscripts to ensure that the operators are applied in the correct order)

提交回复
热议问题