[英]Issue when defining C struct… (AKA frustration)
Hey so I'm simply trying to define a struct. 嘿,所以我只是想定义一个结构。 I'm probably being an idiot, but hey, I'm trying.
我可能是个白痴,但是,我正在尝试。
Doing this: 这样做:
struct Neuron{
float mu;
float stim[10];
float hist[10];
int ns[10000];
float st[10000];
float cup[8][10];
};
struct Neuron nur1;
nur1.mu = -0.7;
Getting this: 得到这个:
error: unknown type name 'nur1'
I do not understand why this is. 我不明白为什么会这样。 It's all in the same .c file.
全部都在同一个.c文件中。 Maybe compilation issues?
也许是编译问题? Simply using gcc my_file.c on mac OS X. <3 <3
只需在Mac OS X上使用gcc my_file.c。<3 <3
You can't just say nur1.mu = -0.7
like that. 您不能像这样说
nur1.mu = -0.7
。 Try putting it in a function (maybe your main
function): 尝试将其放入一个函数(可能是您的
main
函数)中:
int main()
{
nur1.mu = -0.7;
}
You need to have a main function; 您需要具有主要功能; otherwise the complier doesn't know where to look for the start of the first function.
否则,编译器将不知道在哪里寻找第一个函数的开始。 The struct is declared outside of main() with everything else inside, like this:
该结构在main()外部声明,其他所有内部声明,如下所示:
struct Neuron{
float mu;
float stim[10];
float hist[10];
int ns[10000];
float st[10000];
float cup[8][10];
};
int main()
{
struct Neuron nur1;
nur1.mu = -0.7;
printf("%f\n", nur1.mu);
}
If it's just to initialize the struct properly, you can use an initializer. 如果只是为了正确初始化结构,则可以使用初始化程序。 This is allowed at the file-level (using only a constant expression ), or for a local variable.
在文件级别(仅使用一个常量表达式 )或局部变量是允许的。
struct Neuron nur1 = {
.mu = -0.7;
};
The syntax like .mu
is called designated initializer (C99). 像
.mu
这样的语法称为指定的初始化程序 (C99)。
Note this will be performed once during program startup and will also initialize all not explicitly initialized fields to 0
as for any global object (see C11 6.7.9p10 ). 请注意,这将在程序启动期间执行一次,并且还将所有未显式初始化的字段(对于任何全局对象)初始化为
0
(请参阅C11 6.7.9p10 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.