C Allocating Two Dimensional Arrays C Allocating Two Dimensional Arrays unix unix

C Allocating Two Dimensional Arrays


So, first, you're going to have to pass in a pointer to myPipes:

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

Then it's easy:

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

And of course, you'd call it with:

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


short answer: change your innermost malloc to a calloc.

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

What you need to understand is that C doesn't really have a way to allocate a true multidimensional array. Instead, you just have a pointer to an array of pointers. Treat your data structure as such and you will be fine.