Dynamically allocate 2D array of ints
In this problem, I am required to allocate memory ALL AT ONCE. Only one dynamic allocation is allowed. I believe I have code that will allocate all memory needed:
cols and rows are passed in as arguments, (let's assume cols = 4, rows = 3). So now I have one big block of memory that will hold 3 pointers to ints which point to the first int in 3 rows of four ints. P will point to my first pointer to int.
int ** p;
int cols, rows;
p = (int **)malloc(((rows * sizeof(int *)) + (cols * rows * sizeof(int)))
I am having a hard time understanding how to initialize the pointers to ints logically. I believe a looping function is needed. Any advice?