将结构数组嵌套到另一个结构中

时间:2017-07-21 18:35:06

标签: c arrays nested structure

我正在尝试创建一个包含结构数组的结构。

我的代码到目前为止(仅剪切为两个字形)

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

typedef struct glyph_t{
        uint16_t width;
        uint16_t length;
        uint16_t *data;
} glyph_t;

typedef struct font_t{
    uint8_t height;
    glyph_t glyphs[2];
} font_t;


// '9' character
const uint16_t glyph_0_data[] = {
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0xc318,0xef7b,0x79ce,0x7def,0xbef7,0xfbde,0x34a5,0x2842,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x2842,0x7def,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xf39c,0x2000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x6529,0xbef7,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x9294,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x18c6,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xe739,0x0000,0x0000,
    0x0000,0xc739,0xffff,0xffff,0xffff,0xffff,0x34a5,0x0421,0x6108,0x8a52,0x5def,0xffff,0xffff,0xffff,0x96b5,0x0000,0x0000,
    0x0000,0x718c,0xffff,0xffff,0xffff,0x79ce,0x0000,0x0000,0x0000,0x0000,0x0842,0xffff,0xffff,0xffff,0xdfff,0x8210,0x0000,
    0x0000,0xbad6,0xffff,0xffff,0xffff,0x4d6b,0x0000,0x0000,0x0000,0x0000,0x0000,0x59ce,0xffff,0xffff,0xffff,0xaa52,0x0000,
    0x2000,0xdfff,0xffff,0xffff,0xffff,0x6529,0x0000,0x0000,0x0000,0x0000,0x0000,0xef7b,0xffff,0xffff,0xffff,0x518c,0x0000,
    0x2421,0xffff,0xffff,0xffff,0xffff,0x2000,0x0000,0x0000,0x0000,0x0000,0x0000,0x694a,0xffff,0xffff,0xffff,0xb6b5,0x0000,
    0xa631,0xffff,0xffff,0xffff,0x9ef7,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x8631,0xffff,0xffff,0xffff,0xdbde,0x0000,
    0xe739,0xffff,0xffff,0xffff,0x5def,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xe318,0xffff,0xffff,0xffff,0xbef7,0x0000,
    0xe739,0xffff,0xffff,0xffff,0x9ef7,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xc318,0xffff,0xffff,0xffff,0xffff,0x4108,
    0x6529,0xffff,0xffff,0xffff,0xffff,0x2000,0x0000,0x0000,0x0000,0x0000,0x0000,0x8210,0xffff,0xffff,0xffff,0xffff,0xa210,
    0x8210,0xffff,0xffff,0xffff,0xffff,0x6529,0x0000,0x0000,0x0000,0x0000,0x0000,0x4108,0xffff,0xffff,0xffff,0xffff,0xe318,
    0x0000,0x3ce7,0xffff,0xffff,0xffff,0xcf7b,0x0000,0x0000,0x0000,0x0000,0x0000,0x6108,0xffff,0xffff,0xffff,0xffff,0x0421,
    0x0000,0x34a5,0xffff,0xffff,0xffff,0x5def,0xc318,0x0000,0x0000,0x0000,0x0000,0x8210,0xffff,0xffff,0xffff,0xffff,0xc318,
    0x0000,0x494a,0xffff,0xffff,0xffff,0xffff,0xdbde,0x694a,0x6108,0x6108,0x0842,0x75ad,0xffff,0xffff,0xffff,0xffff,0x6108,
    0x0000,0x0000,0x59ce,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x0000,
    0x0000,0x0000,0x8631,0x9ef7,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x9ef7,0x0000,
    0x0000,0x0000,0x0000,0x0842,0x5def,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x59ce,0x0000,
    0x0000,0x0000,0x0000,0x0000,0xa210,0x1084,0x9ad6,0x9ef7,0x9ef7,0xbad6,0x9294,0x55ad,0xffff,0xffff,0xffff,0x34a5,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x75ad,0xffff,0xffff,0xffff,0xef7b,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1ce7,0xffff,0xffff,0xffff,0x0842,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x8631,0xffff,0xffff,0xffff,0x9ef7,0x2000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x718c,0xffff,0xffff,0xffff,0x75ad,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xc318,0xbef7,0xffff,0xffff,0xffff,0x694a,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x75ad,0xffff,0xffff,0xffff,0x9ad6,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x718c,0xffff,0xffff,0xffff,0xffff,0xcb5a,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x2421,0xd7bd,0xffff,0xffff,0xffff,0xffff,0xf7bd,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x6108,0x8631,0x4d6b,0xb6b5,0xffff,0xffff,0xffff,0xffff,0xffff,0x3ce7,0xe318,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x38c6,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x5def,0x8631,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x55ad,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x9ad6,0x2421,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x718c,0xffff,0xffff,0xffff,0xffff,0x7def,0xcf7b,0x2000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x8e73,0x5def,0x79ce,0xf39c,0xaa52,0x4108,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000 }; //glyph_0_data

glyph_t glyph_0 = {
    .width = 17,
    .length = 782,
    .data = glyph_0_data
};


// 'A' character
const uint16_t glyph_1_data[] = {
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xeb5a,0xffff,0xffff,0xffff,0xffff,0x3084,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb6b5,0xffff,0xffff,0xffff,0xffff,0xdbde,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x8210,0xdfff,0xffff,0xffff,0xffff,0xffff,0xffff,0x6529,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xeb5a,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x1084,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x96b5,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xbad6,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x4108,0xbef7,0xffff,0xffff,0x79ce,0x9ad6,0xffff,0xffff,0xffff,0x2421,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xaa52,0xffff,0xffff,0xffff,0x1084,0x1084,0xffff,0xffff,0xffff,0xae73,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x14a5,0xffff,0xffff,0xffff,0x8631,0x8631,0xffff,0xffff,0xffff,0x18c6,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x7def,0xffff,0xffff,0x1ce7,0x0000,0x0000,0x1ce7,0xffff,0xffff,0xffff,0x8210,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xc739,0xffff,0xffff,0xffff,0xb294,0x0000,0x0000,0xd39c,0xffff,0xffff,0xffff,0xcb5a,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3084,0xffff,0xffff,0xffff,0x694a,0x0000,0x0000,0x8a52,0xffff,0xffff,0xffff,0x34a5,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x79ce,0xffff,0xffff,0xdfff,0x6108,0x0000,0x0000,0x6108,0xdfff,0xffff,0xffff,0x7def,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0xa210,0xffff,0xffff,0xffff,0xf7bd,0x0000,0x0000,0x0000,0x0000,0x18c6,0xffff,0xffff,0xffff,0xa631,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0xeb5a,0xffff,0xffff,0xffff,0xcf7b,0x0000,0x0000,0x0000,0x0000,0x1084,0xffff,0xffff,0xffff,0xef7b,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x14a5,0xffff,0xffff,0xffff,0xc739,0x0000,0x0000,0x0000,0x0000,0x0842,0xffff,0xffff,0xffff,0x18c6,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x3ce7,0xffff,0xffff,0x9ef7,0x2000,0x0000,0x0000,0x0000,0x0000,0x4108,0xdfff,0xffff,0xffff,0xdfff,0x6108,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x4529,0xffff,0xffff,0xffff,0xb6b5,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x18c6,0xffff,0xffff,0xffff,0x494a,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x6d6b,0xffff,0xffff,0xffff,0xae73,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1084,0xffff,0xffff,0xffff,0x718c,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x96b5,0xffff,0xffff,0xffff,0xa631,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0842,0xffff,0xffff,0xffff,0x9ad6,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x9ef7,0xffff,0xffff,0x9ef7,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x4108,0xdfff,0xffff,0xffff,0xffff,0x8210,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x8631,0xffff,0xffff,0xffff,0xb6b5,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x18c6,0xffff,0xffff,0xffff,0x8a52,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x8e73,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x9294,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x96b5,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x79ce,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x7def,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0xffff,0x6108,0x0000,0x0000,
    0x0000,0x0000,0x6529,0xffff,0xffff,0xffff,0x55ad,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x75ad,0xffff,0xffff,0xffff,0x494a,0x0000,0x0000,
    0x0000,0x0000,0x4d6b,0xffff,0xffff,0xffff,0xcf7b,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xef7b,0xffff,0xffff,0xffff,0x3084,0x0000,0x0000,
    0x0000,0x0000,0x34a5,0xffff,0xffff,0xffff,0x494a,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x694a,0xffff,0xffff,0xffff,0xf7bd,0x0000,0x0000,
    0x0000,0x0000,0xfbde,0xffff,0xffff,0xffff,0xa210,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xe318,0xffff,0xffff,0xffff,0xbef7,0x2000,0x0000,
    0x0000,0xc318,0xffff,0xffff,0xffff,0x3ce7,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x7def,0xffff,0xffff,0xffff,0xa631,0x0000,
    0x0000,0x694a,0xffff,0xffff,0xffff,0xd7bd,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x18c6,0xffff,0xffff,0xffff,0x6d6b,0x0000,
    0x0000,0x3084,0xffff,0xffff,0xffff,0x718c,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xb294,0xffff,0xffff,0xffff,0x34a5,0x0000,
    0x0000,0xd7bd,0xffff,0xffff,0xffff,0x2c63,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x4d6b,0xffff,0xffff,0xffff,0xfbde,0x0000,
    0x0000,0x7def,0xffff,0xffff,0xffff,0xc739,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0xc739,0xffff,0xffff,0xffff,0xffff,0xa210,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
    0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
}; //glyph_1_data

glyph_t glyph_1 = {
    .width = 24,
    .length = 1104,
    .data = glyph_1_data
};

font_t font = {
    .height = 48, 
    .glyphs[] = {glyph_0, glyph_1}
};

int main()
{
    printf("\n");
    printf("%d\n", font.height);
    printf("%d\n", font.glyph[0].width);
    printf("%d\n",font.glyph[0].data[106]);
    printf("%d\n", font.glyph[1].width);
    printf("%d\n",font.glyph[1].data[106]);
}

编译时,我收到以下错误:

font2.c:133:2: error: array index in initializer exceeds array bounds
  .glyphs[2] = {glyph_0, glyph_1}
  ^
font2.c:133:2: error: (near initialization for ‘font.glyphs’)
font2.c:133:2: error: initializer element is not constant
font2.c:133:2: error: (near initialization for ‘font.glyphs[0].width’)
font2.c:133:2: error: initializer element is not constant
font2.c:133:2: error: (near initialization for ‘font.glyphs[0].length’)

我也尝试过:

font_t font;
font.height = 48;
font.glyphs[0] = glyph_0;
font.glyphs[1] = glyph_1;

编译时出现以下错误:

 gcc -o test font2.c 
font2.c:132:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 font.height = 48;
     ^
font2.c:133:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 font.glyphs[0] = glyph_0;
     ^
font2.c:134:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
 font.glyphs[1] = glyph_1;

我还尝试了"glyphs[2]""glyphs[]"的各种组合但没有成功。我不知道这两个错误意味着什么。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

初始化font时,您使用的语法无效:

font_t font = {
    .height = 48, 
    .glyphs[] = {glyph_0, glyph_1}
    //     ^^ ---- here
};

字段glyphs已知是一个数组,因此不需要下标运算符。

但是,即使您这样做仍然会出现错误,因为font被定义为文件范围,而您用于初始化的值包含其他变量名称而不是常量。

分配每个字段的语法有效,但仅限于函数内。赋值是一个可执行语句,它们不能存在于函数之外。

在文件范围内定义变量而不进行初始化,然后在main中设置字段:

font_t font;

int main()
{
    font.height = 48;
    font.glyphs[0] = glyph_0;
    font.glyphs[1] = glyph_1;

    printf("\n");
    ...

答案 1 :(得分:1)

您的其他答案解释了您的方法中的语法和语义问题;请不要接受这个。我只想添加你可以在文件范围内执行初始化,只要你愿意使用文字。这对我来说似乎是合理的,因为我不知道为什么你想复制你的原始代码试图做的字形数据。它可能看起来像这样:

font_t font = {
    .height = 48, 
    .glyphs = {
        { .width = 17, .length = 782,  .data = glyph_0_data },
        { .width = 24, .length = 1104, .data = glyph_1_data }
    }
};