create_and_modify_array内部の mはローカル変数であるため、セグメンテーション違反が発生するため、内部の mmainはまだ初期化されていません。
より明確にするために、コードフローは次のとおりです。
の先頭はmain m、割り当てられていないランダムなメモリ アドレスです。create_and_modify_array次に、そのメモリアドレスで呼び出します。内部create_and_modify_arrayには、呼び出された新しい変数mが作成され、渡されたランダムで割り当てられていないメモリアドレスがあります。次に、呼び出して、内部arrayの変数が割り当てられたメモリに割り当てられます。mcreate_and_modify_array
問題は、 の値がin にm戻されないことです。mmain
mこれを行うには、mainへのポインターを に渡す必要がありますcreate_and_modify_array。
void create_and_modify_array (double **m) {
double *tmp = array(10);
tmp[0] = 123;
*m = tmp; // This passes the value of tmp back to the main function.
// As m in this function is actually the address of m in main
// this line means put tmp into the thing that is at this address
}
void main () {
double *m;
create_and_modify_array (&m); // This passes the address of the variable m
// into create_and_modify_array
printf ("%f\n", m[0]);
}