初始化结构的问题

时间:2014-05-10 18:53:12

标签: c struct static structure

以下是我正在使用的(部分)结构;它们位于.h文件中:

struct rss_s {
    Radio_types device_type;    // Its device_type which is defined by the typedef above Radio_Types
    char * device_info;      // some thing about the radio NAV/COM/etc.
    char * device_model;     // the Manufactures part/model number.
    char * device_serial;    // the device's serial number..
    int power_48v;           // power to the unit..
    int power_400hz;
    int panel_lamps;         // turn off or on the Panel Lamps only
    void * radio_info;
};

typedef struct tuner_s {    // when we talk about 'sub-radios' we are really saying how many tuners are there??
    char * device_name;    // OS-name
    int frequency[tuned];
    int power;
    int dial_lamp;
    int fd[ ];      // file descriptors
}tuner;

//// 614L8 ::= C614L8

typedef enum Lp_Sw_614L8 { OFF_loop, LEFT, RIGHT, SLEW_LEFT, SLEW_RIGHT } loopsw_614L8;
typedef enum Mo_Sw_614L8 { OFF_614L8, ADF, ANT, LOOP } modesw_614L8;

struct radio_s_614L8 {
    loopsw_614L8 loop_sw_614L8;
    modesw_614L8 mode_sw_614l8;
    int sw_band;
    int sw_bfo;
    int meter;
    tuner * Tuner;
    int tuners;
};      

现在归档main.c,其中包含所有常规包含:

// Radio 614L8<br>

static struct radio_s_614L8 radio_614L8 = { { .Tuner = tuner_614L8, .tuners = DIM( tuner_C_614L8 ) } };
static tuner tuner_614L8 = { { .device_name = "/dev/TBD", }  };

static struct rss_s radios[] = {
    { C614L8, "ADF", "614L8", "8384", & radio_C_614L8,},};

// now comes the normal main()

我遇到的错误:

  • 错误:字段名称不在记录或联合初始化程序中
  • 错误:(接近初始化'radio_614L8.loop_sw_614L8')
  • 错误:'tuner_614L8'未声明此处(不在函数中)
  • 错误:字段名称不在记录或联合初始化程序中
  • 错误:(接近初始化'radio_614L8.loop_sw_614L8')
  • 错误:'tuner_C_614L8'未声明在这里(不在函数中)
  • 错误:字段名称不在记录或联合初始化程序中
  • 错误:(接近初始化for'tuner_614L8.device_name')
  • 错误:'radio_C_614L8'未声明此处(不在函数中)

  • 1 个答案:

    答案 0 :(得分:1)

    您目前有:

    static struct radio_s_614L8 radio_614L8 = { { .Tuner = tuner_614L8, .tuners = DIM( tuner_C_614L8 ) } };
    static tuner tuner_614L8 = { { .device_name = "/dev/TBD", }  };
    

    你需要:

    static tuner tuner_614L8 =  { .device_name = "/dev/TBD", };
    static struct radio_s_614L8 radio_614L8 = { .Tuner = &tuner_614L8, .tuners = 1 };
    

    在您定义或声明之前,您无法引用tuner_614L8之类的变量。您也不应该尝试将非数组转换为数组。您也需要获取调谐器的地址。您没有显示DIM,但我认为它或多或少是这两个等效宏中的一个:

    #define DIM(x)  (sizeof(x)/sizeof(*(x)))
    #define DIM(x)  (sizeof(x)/sizeof((x)[0]))
    

    进一步分析,您的tuner结构包含一个灵活的数组成员。你不能明智地将这些变量分配为静态或全局变量,或者作为自动变量;你必须用malloc()和亲戚分配它们才能获得非空数组。

    然而,考虑到这一点,本代码编译:

    typedef enum Radio_types { C614L8 } Radio_types;
    enum { tuned = 5 };
    
    typedef struct tuner_s
    {
        char *device_name;
        int frequency[tuned];
        int power;
        int dial_lamp;
        int fd[];
    } tuner;
    
    typedef enum Lp_Sw_614L8 { OFF_loop, LEFT, RIGHT, SLEW_LEFT, SLEW_RIGHT } loopsw_614L8;
    typedef enum Mo_Sw_614L8 { OFF_614L8, ADF, ANT, LOOP } modesw_614L8;
    
    struct radio_s_614L8
    {
        loopsw_614L8 loop_sw_614L8;
        modesw_614L8 mode_sw_614l8;
        int sw_band;
        int sw_bfo;
        int meter;
        tuner *Tuner;
        int tuners;
    };
    
    static tuner tuner_614L8 = { .device_name = "/dev/TBD", };
    static struct radio_s_614L8 radio_614L8 = { .Tuner = &tuner_614L8, .tuners = 1 };
    
    struct rss_s
    {
        Radio_types device_type;
        char *device_info;
        char *device_model;
        char *device_serial;
        int power_48v;
        int power_400hz;
        int panel_lamps;
        void *radio_info;
    };
    
    struct rss_s radios[] =
    {
        { C614L8, "ADF", "614L8", "8384", 0, 0, 0, &radio_614L8, },
    };