C90:我如何在全球范围初始化这个结构的C时不C99扩展 [英] C90: How do I globally initialize this struct in C without C99 extensions

查看:217
本文介绍了C90:我如何在全球范围初始化这个结构的C时不C99扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道初始化这个结构的最佳方式是什么C90,同时仍保持平整。

在我的头文件,称它为test.h,我有以下结构定义的:

 结构s_test_cfg {
      字符* A [3];
      炭* B [3];
      字符* C [3];
 }

然后我把它声明为一个外部的结构,这样我可以在.c文件全局初始化:

 的extern结构s_test_cfg test_cfg;

现在在我的.c文件,我希望能够宣布这样的全球性(显然就是我要写在C90不支持):

 结构s_test_cfg test_cfg =
 {.A = {一,B,C},\\
   .B = {D,E,F},\\
   .C = {G,H,I}};

这显然使得它非常整齐而透明的,以你想要做什么。我怎样才能初始化在我的C文件中的全局结构,这也是那样干净这个语法?谢谢你。


解决方案

 结构s_test_cfg test_cfg = {
    {一,B,C},/ * .A * /
    {D,E,F} / * * .B /
    {G,H,I} / * * .C /
};

也许是最简洁的选项(短让自己一个C99编译器,GCC和英特尔C都支持C99)。

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.

解决方案

struct s_test_cfg test_cfg = {
    { "a", "b", "c" },  /* .a */
    { "d", "e", "f" },  /* .b */
    { "g", "h", "i" },  /* .c */
};

is probably the cleanest option (short of getting yourself a C99 compiler; GCC and Intel C both support C99).

这篇关于C90:我如何在全球范围初始化这个结构的C时不C99扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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