【问题标题】:Problems Initializing Structures初始化结构的问题
【发布时间】:2014-05-10 18:53:12
【问题描述】:

这是我正在使用的(一些)结构;它们位于.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”(不在函数中)
  • 错误:字段名不在记录或联合初始化程序中
  • 错误:(接近初始化‘tuner_614L8.device_name’)
  • 错误:此处未声明“radio_C_614L8”(不在函数中)
  • 【问题讨论】:

      标签: c struct static structure


      【解决方案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, },
      };
      

      【讨论】:

      • 好的,我现在已经翻转了它.. 就像你建议的那样...... DIM 是
        #define DIM( a ) ( sizeof( a )/sizeof( a[0] )) 但是我仍然有错误:(
      • 是的;使用可编译代码查看更新。对于预期用途(其中参数a 是一个数组名称),您对DIM 的定义很好。偏执狂会在sizeof((a)[0]) 中在a 周围使用括号,滥用时会以不同的方式中断。
      • 静态调谐器 tuner_614L8 = { { .device_name = "/dev/TBD", } };静态结构 radio_s_614L8 radio_614L8 = { { .Tuner = tuner_614L8, .tuners = DIM(tuner_614L8 ) } };仍然遇到很多错误
      • 注意{{ … }}对于单个结构是错误的;它仅在数组中需要。那是固定的两次。而且您不能将DIM 应用于非数组。将我的代码逐字复制到x394.c 并编译它:gcc -O3 -g -std=c11 -Wall -Wextra -Werror -c x394.c
      • 坚持我把我的代码放在我的网站上(它现在工作错字:(....phoenixaerospace.us/tmp/NexGen
      猜你喜欢
      • 2010-11-10
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 2020-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多