【发布时间】:2018-07-11 20:19:29
【问题描述】:
我写了以下代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct _NeuralNetwork{
int input_rows;
int input_columns;
double **inputs;
}NeuralNetwork;
void main(){
// structure variable
NeuralNetwork *nn;
int count;
int i,j;
nn->input_rows = 2;
nn->input_columns = 3;
// create the array of double pointers using # of rows
nn->inputs = (double **)malloc(nn->input_rows * sizeof(double *));
// each pointer gets an array of double values
for (i=0; i<nn->input_rows; i++){
nn->inputs[i] = (double *)malloc(nn->input_columns * sizeof(double));
}
// assign values
count = 0;
for (i = 0; i < nn->input_rows ; i++)
for (j = 0; j < nn->input_columns; j++)
nn->inputs[i][j] = ++count;
// print those values
for (i = 0; i<nn->input_rows; i++)
for (j = 0; j < nn->input_columns; j++)
printf("%f ", nn->inputs[i][j]);
/* Code for further processing and free the
dynamically allocated memory*/
return;
}
当我编译它时,一切都好。但是运行之后,我得到一个分段错误错误:
分段错误(核心转储)
我不确定,错误在哪里。有人可以帮忙吗?
注意:当我使用 nn 作为结构变量而不是结构时,一切都很好。但我想将它用作结构指针并通过“->”而不是通过“。”访问结构成员。因为我打算稍后将nn 作为指向另一个函数的指针传递。
提前谢谢你:)
【问题讨论】:
-
nn指针在哪里初始化? -
我完全忘记了。正如 SenselessCoder(见下文)所说,这是错误的。
标签: c arrays pointers multidimensional-array structure