App crashes but I can't see the reason. I've been examining the code for many times, but still there is a problem.
I have 2d array 'a'(NxM) and I have to transpose it. The new array is 'p'(MxN).
Here is part of program:
/// 7: Transposing array. ( NxM ---> MxN ).
int **p = NULL;
p = (int **)malloc(M*sizeof(int *));
if ( NULL == p)
{
printf("Failed to allocate memory.");
return 1;
}
for ( i = 0; i < M; i++ )
p[i] = (int *)malloc(N*sizeof(int ));
// Filling in the new array called "p" with '0's.
for ( i = 0; i < M; i++ )
for ( j = 0; j < N; j++ )
p[i][j] = 0;
// Transposing array.
for ( i = 0; i < N; i++ )
{
for ( j = 0; j < M; j++ )
{
a[i][j] = p[j][i];
}
}
// Displaying ARRAY
printf(">>>\n\n");
for ( i = 0; i < M; i++ )
{
for ( j = 0; j < N; j++ )
printf("%4d ", p[i][j]);
printf("\n");
}
Tell me what is wrong, please.
UPDATE: I apologize for the mistake I've made..
// Filling in the new array called "p" with '0's.
for ( i = 0; i < M; i++ )
for ( j = 0; j < N; j++ )
p[i][j] = 0; // there will be 'p', not 'a'.