为什么不能全局定义结构成员? [英] Why can't you define struct members globally?

查看:38
本文介绍了为什么不能全局定义结构成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你全局定义一个 struct 时,你为什么不能全局定义结构成员(除了使用初始化语法)?我从 clang 得到的错误是 system_1 有一个未知类型名称".

When you define a struct globally, why can't you define the structure members globally as well (outside of using the initializer syntax)? The error I'm getting from clang is that system_1 has an "unknown type name".

如果您在函数中定义结构体,例如 main(),那么您就不会遇到任何问题.

If you define the struct within a function, such as main(), then you don't run into any issues.

typedef struct Light_System {
    int redLightPin;
    int yellowLightPin;
    int blueLightPin;
} Light_System;

Light_System system_1;

# "Light_System system_1 = {4, 0, 0}" works

system_1.redLightPin = 4; # doesn't work

int main(int argc, char *argv[]) {
    # placing everything in here works
    # placing just "system_1.redLightPin = 4;" here makes it work.
    printf("%d\n", system_1.redLightPin);
    return 0;
}

我希望我能够在全局范围内定义结构的成员.

I would expect that I would be able to define the structure's members within the global scope.

推荐答案

全局定义结构体时,为什么不能全局定义结构体成员(除了使用初始化语法)?

When you define a struct globally, why can't you define the structure members globally (outside of using the initializer syntax)?

因为您要做的不是定义初始化.这是一个任务.您可以在全局范围内声明、定义和初始化,但不能分配.

Because what you're trying to do is not a definition or an initialization. It's an assignment. You can declare, define and initialize in global scope, but not assign.

这不仅适用于结构.它也适用于变量.在函数之外,您只能声明和初始化变量.你不能做常规任务,或其他任何事情.我不是 100% 确定细节,但我相当有信心你不能在全局范围内做任何在编译时无法完成的事情.

This is not only for structs. It goes for variables too. Outside a function, you can only declare and initialize variables. You cannot do regular assignments, or anything else for that matter. I'm not 100% sure of the details, but I'm fairly confident that you cannot do anything in global scope that cannot be done during compile time.

所以这行得通:

int x=5;

但不是这个:

int x;
x = 5;

嗯,实际上它是有效的,但会产生关于 warning: data definition has no type or storage classwarning: type defaults to 'int' in declaration of 'x' 的神秘警告 然而,仅仅因为它编译,它不会做你认为它会做的事情.实际上,您不理解的神秘警告通常是一个很好的指标,表明代码将执行您既不想要也不理解的事情.这段代码所做的是x隐式重新声明,然后是初始化.如果之前没有初始化它是允许的,所以这是无效的:

Well, actually it is valid but will yield cryptic warnings about warning: data definition has no type or storage class and warning: type defaults to ‘int’ in declaration of ‘x’ However, just because it compiles, it will not do what you think it will. Actually, cryptic warnings that you don't understand is very often a good indicator that the code will do something that you neither want nor understand. What this code does is an implicit redeclaration of x followed by an initialization. It's allowed if it has not been initialized earlier, so this not valid:

int x = 3;
x = 5; // Not ok, because x is already initialized

这里的错误信息清楚地说明了为什么前面的例子编译:error: redefinition of ‘x’.

And the error message here makes it clear why the previous example compiled: error: redefinition of ‘x’.

同样,这也无效:

int x = foo();  

它给出了这个错误:

main.c:3:7: error: initializer element is not constant
 int x = foo();
         ^~~

这篇关于为什么不能全局定义结构成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆