アプリがクラッシュしますが、理由がわかりません。コードを何度も調べましたが、まだ問題があります。
2次元配列 'a'(NxM) があり、それを転置する必要があります。新しい配列は 'p'(MxN) です。
ここにプログラムの一部があります:
/// 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");
}
何が悪いのか教えてください。
更新:私が犯した間違いをお詫び申し上げます..
// 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'.