C90:如何在没有 C99 扩展的情况下在 C 中全局初始化此结构
我想知道使用 C90 初始化该结构的最佳方法是什么,同时仍然保持整洁。
在我的头文件中,将其称为 test.h,我定义了以下结构:
struct s_test_cfg{
char *a[3];
char *b[3];
char *c[3];
}
然后将其声明为 extern 结构,以便我可以在 .c 文件中全局初始化它:
extern struct s_test_cfg test_cfg;
现在在我的 .c 文件中,我想能够在全局范围内声明类似的内容(显然我将要编写的内容在 C90 中不受支持):
struct s_test_cfg test_cfg =
{ .a = {"a", "b", "c"},\
.b = {"d", "e", "f"},\
.c = {"g", "h", "i"} };
这显然使得您想要做的事情变得非常整洁和透明。如何在我的 C 文件中初始化全局结构,使其与此语法一样干净?谢谢。
I was wondering what the best way to initialize this struct is with C90, while still keeping it neat.
In my header file, call it test.h, I have the following struct defined:
struct s_test_cfg{
char *a[3];
char *b[3];
char *c[3];
}
Then I have it declared as an extern struct so that I can initialize it globally in the .c file:
extern struct s_test_cfg test_cfg;
Now in my .c file, I want to be able to declare something like this globally (obviously what I'm about to write is unsupported in C90):
struct s_test_cfg test_cfg =
{ .a = {"a", "b", "c"},\
.b = {"d", "e", "f"},\
.c = {"g", "h", "i"} };
This obviously makes it very neat and transparent as to what you're trying to do. How can I initialize the global struct in my C file that is also as clean as this syntax? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是最干净的选择(除了给自己一个 C99 编译器之外;GCC 和 Intel C 都支持 C99)。
is probably the cleanest option (short of getting yourself a C99 compiler; GCC and Intel C both support C99).