C 通过引用函数传递复杂结构
C Passing Complex Structures by Reference to Functions
我复制了 here 提供的指南,但在带有函数头和原型的每一行中继续收到以下错误:'typedef "Neuron" may not be used in an elaborated type specifier'。我研究了这个错误,没有找到太多有用的内容。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
struct neuronHead {
int x, y, z;
neuronHead *apical[25];
neuronHead *basal[25];
neuronHead *axon[25];
int time;
} neuron;
typedef struct neuron Neuron;
void setupBrain(struct Neuron ****brain); /* brain is a 3D array of structs */
void freeBrain(struct Neuron ****brain);
int main(void) {
Neuron ***brain;
setupBrain(&brain);
freeBrain(&brain); }
void setupBrain(struct Neuron ****brain) {
/* code for malloc'ing a 3D array of structures */ }
void freeBrain(struct Neuron ****brain) {
/* code for freeing the 3D array of structures */ }
我运行正在 Ubuntu 14.04 上使用 Nvidia 的 NVCC 编译器 运行 GPU 上的代码,尽管这与手头的错误无关。
在您的代码中,没有 struct neuron
您可以输入定义。在您的例子中,neuron
是 struct neuronHead
类型的全局变量。
您需要将密码更改为
struct neuronHead {
int x, y, z;
struct neuronHead *apical[25];
struct neuronHead *basal[25];
struct neuronHead *axon[25];
int time;
};
typedef struct neuronHead Neuron;
或者,
typedef struct neuronHead Neuron;
struct neuronHead {
int x, y, z;
Neuron *apical[25];
Neuron *basal[25];
Neuron *axon[25];
int time;
};
或者,
typedef struct neuronHead {
int x, y, z;
struct neuronHead *apical[25];
struct neuronHead *basal[25];
struct neuronHead *axon[25];
int time;
} Neuron;
考虑创建一个 int
:
的实例
int some_int;
在C
中,一般来说,语法是:
<type> <variable_name>
当你写下这样的东西时:
struct Thing {
int i;
/* Other members... */
} thing;
这实际上的意思是:创建一个变量,命名为thing
,类型为struct Thing
,这个结构定义如下:{ int i; /* Other members... */ }
.
然后你做这样的事情:
typedef struct thing SomeThing;
在这一点上,它没有意义,因为您正在尝试 typedef
一个实例 (thing
)。
你的意思可能是这样的:
struct Thing {
int i;
/* Other members... */
}; /* Nothing here! It's the end of type definition. */
typedef struct Thing thing; /* Typedef "struct Thing" -> "thing". */
typedef
的一般语法是:
typedef <original_type> <new_alias>
所以可以缩短:
typedef /* Typedef the following struct... */
struct Thing {
int i;
/* Other members... */
}
thing; /* ... to such a name. */
那么就可以正确的使用thing
作为变量类型了
也:C
中没有引用,只有指针。
你应该更换
typedef struct neuron Neuron;
和
typedef struct neuronHead Neuron;
写作时
struct neuronHead { ... } neuron;
你介绍两个要素:
一个名为 "struct neuronHead"、
的复合数据类型
和一个名为 "neuron" 的变量,类型为 "struct neuronHead"。
Typedef 允许为数据类型创建别名:
typedef <original_type> <alias_type>;
而在您的示例中,(结构神经元)不是正确的类型。正确的类型定义是:
struct neuronHead { ... } neuron; /* data type AND variable were introduced */
typedef struct neuronHead Neuron; /* data type introduced */
和:
typedef struct neuronHead { ... } Neuron; /* data type AND type alias were introduced */
预定义类型的示例用法:
struct neuronHead n1; /* correct */
Neuron n2; /* also correct */
struct neuron n3; /* incorrect: 'neuron' is variable name! */
谢谢。
我复制了 here 提供的指南,但在带有函数头和原型的每一行中继续收到以下错误:'typedef "Neuron" may not be used in an elaborated type specifier'。我研究了这个错误,没有找到太多有用的内容。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
struct neuronHead {
int x, y, z;
neuronHead *apical[25];
neuronHead *basal[25];
neuronHead *axon[25];
int time;
} neuron;
typedef struct neuron Neuron;
void setupBrain(struct Neuron ****brain); /* brain is a 3D array of structs */
void freeBrain(struct Neuron ****brain);
int main(void) {
Neuron ***brain;
setupBrain(&brain);
freeBrain(&brain); }
void setupBrain(struct Neuron ****brain) {
/* code for malloc'ing a 3D array of structures */ }
void freeBrain(struct Neuron ****brain) {
/* code for freeing the 3D array of structures */ }
我运行正在 Ubuntu 14.04 上使用 Nvidia 的 NVCC 编译器 运行 GPU 上的代码,尽管这与手头的错误无关。
在您的代码中,没有 struct neuron
您可以输入定义。在您的例子中,neuron
是 struct neuronHead
类型的全局变量。
您需要将密码更改为
struct neuronHead {
int x, y, z;
struct neuronHead *apical[25];
struct neuronHead *basal[25];
struct neuronHead *axon[25];
int time;
};
typedef struct neuronHead Neuron;
或者,
typedef struct neuronHead Neuron;
struct neuronHead {
int x, y, z;
Neuron *apical[25];
Neuron *basal[25];
Neuron *axon[25];
int time;
};
或者,
typedef struct neuronHead {
int x, y, z;
struct neuronHead *apical[25];
struct neuronHead *basal[25];
struct neuronHead *axon[25];
int time;
} Neuron;
考虑创建一个 int
:
int some_int;
在C
中,一般来说,语法是:
<type> <variable_name>
当你写下这样的东西时:
struct Thing {
int i;
/* Other members... */
} thing;
这实际上的意思是:创建一个变量,命名为thing
,类型为struct Thing
,这个结构定义如下:{ int i; /* Other members... */ }
.
然后你做这样的事情:
typedef struct thing SomeThing;
在这一点上,它没有意义,因为您正在尝试 typedef
一个实例 (thing
)。
你的意思可能是这样的:
struct Thing {
int i;
/* Other members... */
}; /* Nothing here! It's the end of type definition. */
typedef struct Thing thing; /* Typedef "struct Thing" -> "thing". */
typedef
的一般语法是:
typedef <original_type> <new_alias>
所以可以缩短:
typedef /* Typedef the following struct... */
struct Thing {
int i;
/* Other members... */
}
thing; /* ... to such a name. */
那么就可以正确的使用thing
作为变量类型了
也:C
中没有引用,只有指针。
你应该更换
typedef struct neuron Neuron;
和
typedef struct neuronHead Neuron;
写作时
struct neuronHead { ... } neuron;
你介绍两个要素:
一个名为 "struct neuronHead"、
的复合数据类型
和一个名为 "neuron" 的变量,类型为 "struct neuronHead"。
Typedef 允许为数据类型创建别名:
typedef <original_type> <alias_type>;
而在您的示例中,
struct neuronHead { ... } neuron; /* data type AND variable were introduced */
typedef struct neuronHead Neuron; /* data type introduced */
和:
typedef struct neuronHead { ... } Neuron; /* data type AND type alias were introduced */
预定义类型的示例用法:
struct neuronHead n1; /* correct */
Neuron n2; /* also correct */
struct neuron n3; /* incorrect: 'neuron' is variable name! */
谢谢。