简体   繁体   English

在结构中定义常量

[英]Defining constants inside a structure

Is there any special significance to defining constant data inside a structure as shown. 如图所示,在结构内定义常量数据是否有任何特殊意义。 This is from a 3rd party library. 这是来自第三方图书馆。

typedef struct              
{           
    IntVB abc_number;           
    #define ABC_A   0x01    
    #define ADBC_E  0x02     
    IntVB asset;            
} StructA;

Not really. 并不是的。 They probably provide better significance to the programmer in that spot of the code. 它们可能在程序代码中为程序员提供了更好的意义。

Meaning that those constants are probably related to the items in that struct container, or to the behavior of the struct. 这意味着这些常量可能与该struct容器中的项目或结构的行为有关。

Agree with @Luca Matteis. 同意@Luca Matteis。 They are probably defined there because they are relevant at that point in the code. 它们可能是在那里定义的,因为它们在代码中的那一点是相关的。 The compiler doesn't treat them specially. 编译器不会特别处理它们。 In particular, they could be defined just before that structure and work just the same. 特别是,它们可以在该结构之前定义并且工作方式相同。 There's no significance to them being inside it. 他们在里面没有任何意义。

However, there is one thing to note, they are only valid after they are defined. 但是,有一点需要注意,它们仅定义后才有效。 So they can't be used earlier in the file. 所以它们不能在文件的早期使用。 That could be significant. 这可能很重要。 For instance, they could be defined differently before that point. 例如,在此之前可以对它们进行不同的定义。 (It's a bad idea to do that, but it's possible.) (这样做是个坏主意,但这是可能的。)

不,他们可以在没有任何范围的情况下被召唤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM