【问题标题】:C expression must have pointer to struct or union typeC 表达式必须具有指向结构或联合类型的指针
【发布时间】:2016-07-21 18:51:28
【问题描述】:

我使用的是 CCS v6,出现结构语法错误。

typedef struct _mem_ptr_t
{
    struct _mem_ptr_t *next;    ///< Next pointer in memory
    uint8    alloc;              ///< Allocated
    struct  mmem mmem_ptr;      ///< The actual pointer to the pointer to the mem block
} mem_ptr_t;

struct mmem {
  struct mmem *next;
  unsigned int size;
  void *ptr;
};

以上代码是原始状态。但有一个错误。 "#71 不完整类型是不允许的"

所以,我更改了代码“struct mmem mmem_ptr;”->“struct mmem *mmem_ptr;” 当我编译时,那部分就通过了。

但是另一部分发生了错误。

if ((mem_ptr = mac_scan_alloc()) != NULL) {
        memcpy(&SCAN_ENTRY(mem_ptr)->oord_addr, src_addr, sizeof(address_t));
        SCAN_ENTRY(mem_ptr)->superfrm_spec = superframe_spec;
        SCAN_ENTRY(mem_ptr)->coord_pan_id = src_pan_id;
        SCAN_ENTRY(mem_ptr)->channel = channel;
    }
#define SCAN_ENTRY(m)  ((pan_descr_t *)MMEM_PTR(&m->mmem_ptr)) 

出现错误“#133 expression must have pointer-to-struct-or-union type”

我已经看过关于这个问题的相关问题。但我无法理解解决上述问题。 Expression must have pointer to struct or union error

我应该如何修复我的代码来解决这个问题?

【问题讨论】:

  • SCAN_ENTRY的定义?
  • @EugeneSh.:它已经发布了。无论如何,#define SCAN_ENTRY(m) ((pan_descr_t *)MMEM_PTR(&amp;m-&gt;mmem_ptr)) 可能患有过多的&符号。
  • 对.. 但是为什么在之后使用它呢?
  • @EugeneSh.:这实际上是一个真的好问题......
  • 哦,对不起。这只是混合代码。 SCAN_ENTRY 位于头文件!

标签: c pointers structure


【解决方案1】:

struct _mem_ptr_t 在定义之前使用struct mmem。所以交换定义:

struct mmem {
  struct mmem *next;
  unsigned int size;
  void *ptr;
};

typedef struct _mem_ptr_t
{
    struct _mem_ptr_t *next;    ///< Next pointer in memory
    uint8    alloc;              ///< Allocated
    struct  mmem mmem_ptr;      ///< The actual pointer to the pointer to the mem block
} mem_ptr_t;

mmem_ptr 的定义从struct mmem 更改为struct mmem * 不起作用,因为您正在更改其类型,因此使用它的任何代码都无法正确执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多