I have some troubles understanding pointers and dynamic memory allocation. I wrote those 2 codes:
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
for(i = 0; i < nrows; i++)
{
*(array+i) = malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
}
and:
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
for(i = 0; i < nrows; i++)
{
array[i] = malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
}
They should allocate space for 2 dimensional array. Although Im not sure if theyre both correct, I mean: does this line:
array[i] = malloc(ncolumns * sizeof(int));
do the exact same thing line this one:
*(array+i) = malloc(ncolumns * sizeof(int));
?