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

c – 数组数组的指定初始化程序,第1张

概述我们可能知道c99中的 designated initializers以及它们可用于初始化大型const数组的方式: const int vals[] = { [0] = 10, [10] = 12} 将val的索引0初始化为10,将val的索引10归为12. 我的问题是如何将这种类型的指定初始化器用于数组数组.这是我的示例代码: typedef enum { COMMO 我们可能知道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,

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

总结

以上是内存溢出为你收集整理的c – 数组数组的指定初始化程序全部内容,希望文章能够帮你解决c – 数组数组的指定初始化程序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1217578.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存