struct A的数组的静态初始化,其中struct A包含一个指向struct B数组的指针(在C中)

时间:2021-08-31 05:12:14

Given the following typedefs:

鉴于以下类型定义:

// Structures for kana to romaji conversion lookup
typedef struct {
  const u16 kana; // Kana codepoint
  const char* romaji;
} KanaSuffix;

typedef struct {
  // Kana codepoint is implied by position in array
  const char* romaji;
  const KanaSuffix* suffixes;
} KanaPrefix;

Is it possible to initialize an array of KanaPrefix statically in a single step, where some elements of the array have suffixes pointing to a static array of KanaSuffix?

是否可以在单个步骤中静态地初始化KanaPrefix数组,其中数组的一些元素有指向KanaSuffix的静态数组的后缀?

Now I'm doing this:

现在我这样做:

const KanaSuffix KANA_SUFFIXES_KI[] = {
  { 0x3030, "kya" },
  { 0x3032, "kyo" }
};

const KanaPrefix KANA_TO_ROMAJI[] = {
  { NULL, NULL },
  { "a", NULL },
  { "ki", KANA_SUFFIXES_KI }
};

But I want to do something more like this:

但我想做的是:

const KanaPrefix KANA_TO_ROMAJI[] = {
  { NULL, NULL },
  { "a", NULL },
  { "ki", {
    { 0x3030, "kya" },
    { 0x3032, "kyo" }
  } }
};

2 个解决方案

#1


6  

You can do something like:

你可以这样做:

const KanaPrefix KANA_TO_ROMAJI[] = {
  { NULL, NULL },
  { "a", NULL },
  { "ki",  (KanaSuffix [2]) {
      { 0x3030, "kya" },
      { 0x3032, "kyo" }
    }
  }
};

EDIT:

编辑:

I can [now] confirm this is defined behaviour, since the lifetime [or duration] of that compound literal is the same as of static storage:

我可以[现在]确认这是已定义的行为,因为该复合文字的生存期[或持续期]与静态存储相同:

C99 §6.5.2.5 Compound literals

C99§6.5.2.5复合文字

The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block.

复合文字的值是初始化器列表初始化的未命名对象的值。如果复合文字出现在函数的主体之外,则对象具有静态存储时间;否则,它具有与封装块相关联的自动存储时间。

References:

引用:

What is the lifetime of compound literals passed as arguments?

作为参数传递的复合文字的生命周期是多少?

#2


6  

If all you need is a static object, then you can use compound literals, since if defined outside of a function scope, have static storage duration.

如果您所需要的只是一个静态对象,那么您可以使用复合文字,因为如果定义在函数范围之外,则具有静态存储时间。

Of course since the member suffixes of the struct KanaPrefix is pointing to more than one element you will need to store the count too:

当然,由于struct KanaPrefix的成员后缀指向多个元素,所以您也需要存储计数:

typedef struct {
  const char* romaji;
  const KanaSuffix* suffixes;
  const size_t count;   
} KanaPrefix;

const KanaPrefix KANA_TO_ROMAJI[] = {
  { NULL, NULL , 0 },
  { "a", NULL , 0 },
  { "ki", ( KanaSuffix[2] ){ //this is a compound literal
    { 0x3030, "kya" },
    { 0x3032, "kyo" }
  } , 2 }  //count is here
};

#1


6  

You can do something like:

你可以这样做:

const KanaPrefix KANA_TO_ROMAJI[] = {
  { NULL, NULL },
  { "a", NULL },
  { "ki",  (KanaSuffix [2]) {
      { 0x3030, "kya" },
      { 0x3032, "kyo" }
    }
  }
};

EDIT:

编辑:

I can [now] confirm this is defined behaviour, since the lifetime [or duration] of that compound literal is the same as of static storage:

我可以[现在]确认这是已定义的行为,因为该复合文字的生存期[或持续期]与静态存储相同:

C99 §6.5.2.5 Compound literals

C99§6.5.2.5复合文字

The value of the compound literal is that of an unnamed object initialized by the initializer list. If the compound literal occurs outside the body of a function, the object has static storage duration; otherwise, it has automatic storage duration associated with the enclosing block.

复合文字的值是初始化器列表初始化的未命名对象的值。如果复合文字出现在函数的主体之外,则对象具有静态存储时间;否则,它具有与封装块相关联的自动存储时间。

References:

引用:

What is the lifetime of compound literals passed as arguments?

作为参数传递的复合文字的生命周期是多少?

#2


6  

If all you need is a static object, then you can use compound literals, since if defined outside of a function scope, have static storage duration.

如果您所需要的只是一个静态对象,那么您可以使用复合文字,因为如果定义在函数范围之外,则具有静态存储时间。

Of course since the member suffixes of the struct KanaPrefix is pointing to more than one element you will need to store the count too:

当然,由于struct KanaPrefix的成员后缀指向多个元素,所以您也需要存储计数:

typedef struct {
  const char* romaji;
  const KanaSuffix* suffixes;
  const size_t count;   
} KanaPrefix;

const KanaPrefix KANA_TO_ROMAJI[] = {
  { NULL, NULL , 0 },
  { "a", NULL , 0 },
  { "ki", ( KanaSuffix[2] ){ //this is a compound literal
    { 0x3030, "kya" },
    { 0x3032, "kyo" }
  } , 2 }  //count is here
};