And you should free the memory you've allocated, once your done with it
Eg:
Code:
#include <stdlib.h>
int main(void)
{
const size_t rows = 3, cols = 5;
int ** a;
size_t c;
/* allocate rows */
if((a = malloc(rows * sizeof(int *))) == NULL)
{
perror("Failed to allocate rows");
return 1;
}
/* allocate cols */
for(c = 0; c < rows; c++)
{
if((a[c] = malloc(cols * sizeof(int))) == NULL)
{
perror("Failed to allocate cols");
return 2; /* ideally you should free the cols and rows, but cbf :) */
}
}
/* do whatever with rows and cols */
/* free cols */
for(c = 0; c < rows; c++)
{
free(a[c]);
a[c] = NULL;
}
/* free rows */
free(a);
a = NULL;
return 0;
}