私はまだC、malloc、そしてそのすべてのジャズが新しいので、これを書いていくつかのスキルを学ぶことにしました。アイデアは、私はファイルから整数の束を読み込み、それらを行列(2次元配列)に入れています。ファイルの先頭には行数と列数があるので、それらの数値を読み込み、mallocを使って2次元配列を設定します。2次元配列をmallocingするときの問題
int read_matrix(FILE *mat, int ***Z, int *x, int *y)
{
int i = 0;
int x_temp = 0;
int y_temp = 0;
if (fscanf(mat, "%d %d", &(*x), &(*y)) == EOF){
printf("File is not big enough to contain a matrix\n");
return -1;
}
printf("About to malloc %d\n", *x);
*Z = (int**) malloc(*x * sizeof(int*));
while (i < *x) {
printf("mallocing %d\n", i);
*Z[i] = (int*) malloc(*y * sizeof(int));
printf("malloced\n");
++i;
}
printf("Malloc complete\n");
/*Other unimportant code*/
}
出力が読み:
About to malloc 3
mallocing 0
malloced
mallocing 1
Segmentation fault
だから..私は思うZ内の1つのint型**が、何もmallocingないのですか?
私はCについてとても新しいので、少し間違いがあるかどうか、あるいは本当に間違っているかどうかはわかりません。何かご意見は?ありがとう!
'&(* x)'は 'x'と同じです。 – vz0
Ahhhhh yea、ちょうど私が馬鹿hahahであるとして(&x)を無視します。 – cost