加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 数组数组的指定初始化程序

发布时间:2020-12-16 10:05:33 所属栏目:百科 来源:网络整理
导读:我们可能知道c99中的 designated initializers以及它们可用于初始化大型const数组的方式: const int vals[] = { [0] = 10,[10] = 12} 将val的索引0初始化为10,将val的索引10归为12. 我的问题是如何将这种类型的指定初始化器用于数组数组.这是我的示例代码:
我们可能知道c99中的 designated initializers以及它们可用于初始化大型const数组的方式:

const int vals[] = {
    [0] = 10,[10] = 12
}

将val的索引0初始化为10,将val的索引10归为12.

我的问题是如何将这种类型的指定初始化器用于数组数组.这是我的示例代码:

typedef enum {
    COMMON = 0,STRINGS,KEY,PRECUSSIVE,GUITAR,KEYBOARD,BASS,PIANO,DRUMS,_INST_MAX
} instrument_classification_t;
    static const int * const instrument_class_hierarchy[] = {
    [COMMON] = {STRINGS,_INST_MAX},[STRINGS] = {GUITAR,[KEY] = {PIANO,[PRECUSSIVE] = {DRUMS,[GUITAR] = NULL,[KEYBOARD] = NULL,[BASS] = NULL,[PIANO] = NULL,[DRUMS] = NULL
};

但我得到这些警告,暗示初始化只使用第一个元素.

main.c:166:3: warning: braces around scalar initializer
   [COMMON] = {STRINGS,^
main.c:166:3: note: (near initialization for 'instrument_class_hierarchy[0]')
main.c:166:15: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
   [COMMON] = {STRINGS,^
main.c:166:15: note: (near initialization for 'instrument_class_hierarchy[0]')
main.c:166:24: warning: excess elements in scalar initializer
   [COMMON] = {STRINGS,^
main.c:166:24: note: (near initialization for 'instrument_class_hierarchy[0]')
main.c:166:29: warning: excess elements in scalar initializer
   [COMMON] = {STRINGS,^
main.c:166:29: note: (near initialization for 'instrument_class_hierarchy[0]')
main.c:166:41: warning: excess elements in scalar initializer
   [COMMON] = {STRINGS,^
main.c:166:41: note: (near initialization for 'instrument_class_hierarchy[0]')
main.c:167:3: warning: braces around scalar initializer
   [STRINGS] = {GUITAR,^
main.c:167:3: note: (near initialization for 'instrument_class_hierarchy[1]')
main.c:167:16: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
   [STRINGS] = {GUITAR,^
main.c:167:16: note: (near initialization for 'instrument_class_hierarchy[1]')
main.c:167:24: warning: excess elements in scalar initializer
   [STRINGS] = {GUITAR,^
main.c:167:24: note: (near initialization for 'instrument_class_hierarchy[1]')
main.c:167:30: warning: excess elements in scalar initializer
   [STRINGS] = {GUITAR,^
main.c:167:30: note: (near initialization for 'instrument_class_hierarchy[1]')
main.c:168:3: warning: braces around scalar initializer
   [KEY] = {PIANO,^
main.c:168:3: note: (near initialization for 'instrument_class_hierarchy[2]')
main.c:168:12: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
   [KEY] = {PIANO,^
main.c:168:12: note: (near initialization for 'instrument_class_hierarchy[2]')
main.c:168:19: warning: excess elements in scalar initializer
   [KEY] = {PIANO,^
main.c:168:19: note: (near initialization for 'instrument_class_hierarchy[2]')
main.c:168:29: warning: excess elements in scalar initializer
   [KEY] = {PIANO,^
main.c:168:29: note: (near initialization for 'instrument_class_hierarchy[2]')
main.c:169:3: warning: braces around scalar initializer
   [PRECUSSIVE] = {DRUMS,^
main.c:169:3: note: (near initialization for 'instrument_class_hierarchy[3]')
main.c:169:19: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
   [PRECUSSIVE] = {DRUMS,^
main.c:169:19: note: (near initialization for 'instrument_class_hierarchy[3]')
main.c:169:26: warning: excess elements in scalar initializer
   [PRECUSSIVE] = {DRUMS,

解决方法

支撑的值列表无法初始化指针.您的错误与以下内容相同:

int *p = { 1,2,3 };

您可以使用复合文字:

// ...
[COMMON] = (const int[]){STRINGS,

这假设代码在文件范围内(即在任何函数之外),这意味着复合文字具有静态存储持续时间,因此可以安全地指向它们.如果您的代码实际上在函数内部,则需要声明单独的静态数组或进行其他修改.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读