C90:如何在没有 C99 扩展的情况下在 C 中全局初始化此结构

发布于 2024-11-19 03:12:20 字数 554 浏览 4 评论 0原文

我想知道使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2024-11-26 03:12:20
struct s_test_cfg test_cfg = {
    { "a", "b", "c" },  /* .a */
    { "d", "e", "f" },  /* .b */
    { "g", "h", "i" },  /* .c */
};

可能是最干净的选择(除了给自己一个 C99 编译器之外;GCC 和 Intel C 都支持 C99)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文