How to free 2d array in C?
You will have to loop over ptr[i]
, freeing each int*
that you traverse, as you first suggest. For example:
for (int i = 0; i < N; i++)
{
int* currentIntPtr = ptr[i];
free(currentIntPtr);
}
Just the opposite of allocation:
for(int i = 0; i < N; i++)
free(ptr[i]);
free(ptr);