【问题标题】:structures and pointers error: dereferencing pointer to incomplete type结构和指针错误:取消引用指向不完整类型的指针
【发布时间】:2014-07-06 21:43:21
【问题描述】:

好的,我有三个结构:

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;

    struct radio_s_C614L8
    {
        loopsw_614L8 loop_sw_614L8;  this is an emum
        modesw_614L8 mode_sw_614L8;  this is an emum
        int sw_band;
        int sw_bfo;
        int meter;
        tuner *Tuner;
        int tuners;
    };

    typedef struct tuner_s
    {
        char *device_name;      // OS NAME
        int frequency[tuned];
        int power;
        int dial_lamp;
        void * back_radio;      // back-link to radios[n]
        void * back_info;       // back-link to radio_xxxx
        int fd[];
    } tuner;

我在 main.c 中初始化它们

// Radio 614L8
static tuner tuner_C614L8[] = {{ .device_name = "/dev/TBD", }};
static struct radio_s_C614L8  radio_C614L8 = { .Tuner = &tuner_C614L8, .tuners =   DIM(tuner_C614L8) };

static struct rss_s radios[] = {
{ .device_type  = C614L8,
  .device_info  = "ADF",
  .device_model = "614L8",
  .device_serial = "8384",
  .radio_info = &radio_C614L8,},};

上面的工作没有错误.... 但是当我尝试在我的 init_C614L8.c 中初始化上述收音机时 使用以下代码我得到一个错误... 错误:在第 4 行和第 6 行中取消引用指向不完整类型的指针

int init_C614L8( struct rss_s  * radios ){
int rw, i;
struct radio_s_614L8 * rad_info = radios -> radio_info;
tuner * this_tuner  = rad_info -> Tuner;
    // Now we will loop over the sub_devices....
for ( i = 0;  i < rad_info -> tuners; i++ ) {

我想我必须投一些东西,但不是 shure 谢谢

【问题讨论】:

  • 你真的需要发布所有这些代码吗?包括the shortest program necessary to reproduce the problem
  • 如果我不向您展示我的代码和结构,您将如何做出明智的评论。
  • 我的意思是它不够短。您真的需要发布所有字段名称,例如power_400hzmodesw_614L8?制作一个简短的程序来重现问题。这样做可能会发现错误。
  • 您是否意识到radio_s_C614L8tuner_s嵌套rss_s 中,在发布的代码中没有关闭}?正确缩进您发布的代码将显示:
  • 请不要在-&gt;运算符的箭头周围发布带有空格的C代码。滥用这样的语法是不礼貌的。我知道您在创建问题时曾被要求创建 MCVE (How to create a Minimal, Complete, and Verifiable Example?) 或 SSCCE (Short, Self-Contained, Correct Example) — 我知道,因为我提出了问题。如果我们无法获取问题中的代码并在其上运行编译器,则会大大增加为您提供答案的难度。

标签: c pointers struct casting


【解决方案1】:

rss.h 你声明

struct radio_s_C614L8

但是在init_C614L8.c你使用

struct radio_s_614L8

无处声明。

更新:

修复这个错误

错误:取消引用指向不完整类型的指针

init_C614L8.c(和任何其他地方,但rss.h)替换

struct radio_s_614L8

通过

struct radio_s_C614L8

这里学到的教训是要么戴上眼镜,要么睡一觉! ;-) 还有:“编译器从不撒谎!”

【讨论】:

  • 如果这解决了您的问题,您可能希望标记一个答案,以便 SO 将问题显示为已回答。
  • 不.. 还在那里.. :( 你看过我网站上的代码了吗??它在我的网络服务器上:www.phoenixaerospace.us/tmp/NexGen/
  • @Phoenixcomm 是的,我做到了。
【解决方案2】:

这意味着struct radio_s_614L8 的定义对于出现错误的代码是不可见的。您要么忘记了 include 定义,要么有 #if... 指令删除了定义,或者您认为存在 includes。

【讨论】:

  • 我所有的结构定义都在 rss.h 中,即 #include "rss.h" 在我的代码的每一页上。并且所有初始化都在模块 main.c 中完成,但在我的 main 函数之上。我的代码中没有#if(s)
  • @Phoenixcomm 好吧,这不会改变错误。编译器将struct radio_s_614L8 视为不完整的类型。这意味着它不知道struct 中的内容。无论你做什么来提供它都不起作用。除非您发布完整的代码,否则没有什么可以说的。
  • 哦,你想看看我的代码......它在我的网络服务器上:www.phoenixaerospace.us/tmp/NexGen/
猜你喜欢
  • 2017-02-24
  • 2016-07-12
  • 2013-12-22
  • 2021-07-25
  • 1970-01-01
  • 2011-04-06
  • 1970-01-01
相关资源
最近更新 更多