#include #include typedef struct NEURON { double output; int Ninp; double *weights; struct NEURON **input_neuron; } neuron; #define N_OF_NEURON 3 int main(int argc, char **argv) { int i; neuron y, x[N_OF_NEURON]; /* 結合係数の初期化,すべて 1.0 とする */ y.weights=(double *)malloc(N_OF_NEURON * sizeof(double)); for ( i=0; i < N_OF_NEURON; i++ ) { y.weights[i] = 1.0; } /* neuron 構造体へのポインタの配列の動的メモリ割り付け */ y.input_neuron = (neuron **)malloc(N_OF_NEURON * sizeof(neuron *)); for ( i=0; i < N_OF_NEURON; i++) { y.input_neuron[i] = &x[i]; /* 各結合を表す */ } /* 各入力ニューロン x[0], x[1], x[2] の出力値を設定 */ /* 下の 3 つの書式は C の文法上問題がない */ x[0].output = 1.0; (x+1)->output = 10.0; (*(x+2)).output = 100; /* 正しくポインタが動作するかの確認 */ for ( i=0; i < N_OF_NEURON; i++) { printf("y.input_neuron[%d]->output=%f\n", i, y.input_neuron[i]->output); } /* y の出力値の計算 */ y.output = 0.0; for ( i=0; i < N_OF_NEURON; i++ ) { y.output += y.weights[i] * y.input_neuron[i]->output; } printf("The output of neuron y is %f.\n", y.output); return 0; }