出力を理解する際にはどう考えるべきですか?なぜなら、私の出力は20の整数のためのガベージであり、私は理由を知りません。私の目的は、それぞれ30個の整数で20個の配列を作成することです。だから、最後の配列は何を取得していることは、正確ではありませんmalloc(c)に関する私の出力にはわかりません
私は...私は右のあなたの質問を得た願っています48
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int **p;//Declaration of a pointer variable
int i = 0, j;
int rows = 20;
int columns = 30;
p = (int**)malloc(20 * sizeof(int)); //First "bookend" allocates space
printf("Hello World! I have created a dynamic 20-array of 20x30 integers!\n");
if (p == NULL)
{
printf("Failed to allocated memory!");
exit(1);
}
for (i = 0; i < 20; i++)
{
if (p[i] == NULL)
{
printf("Integers not allocated! ");
}
p[i] = (int**)malloc(20 * sizeof(int));
}
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
if (p[j] == NULL)
{
printf("Integers not allocated! ");
}
p[i][j] = (int *)malloc(40 * sizeof(int));
}
printf("%d\n", p[(i+1)+j]);
}
free(p);
return 0;
}
注: '20!= 30' ...と' 20!= 40' – wildplasser
ありがとうございます。私はまだ住所を印刷しています。 (私は余分なforループを削除しました) –