错误:初始化类型时不兼容的类型

时间:2017-10-27 15:03:08

标签: c pointers casting

error: incompatible types when initializing type 'REQ_ONE * {aka struct REQ_ONE *}' using type 'REQ_ONE {aka volatile struct REQ_ONE}'
 #define FIND_T(a, type)  (*((volatile type *)a))

我想获取REQ_ONE的内容。最初我只是在使用:

REQ_ONE *reqReg = FIND(ADDRESS)

但我的代码分析器抱怨pointer to void not allowed。所以我想抛出结构的类型来解决这个问题。我有很多不同的结构,所以我想只使用一个宏:

REQ_ONE *reqReg = FIND_W(ADDRESS, REQ_ONE);

并传递我想要的类型参数。但是,在我看到我的分析工具是否满意之前,我得到了上面的编译错误。

#define TEST_ADDR   0x00652000UL
#define ADDRESS     0x00000500UL

#define FIND(v) ((void *)TEST_ADDR + v)      
#define FIND_T(a, type)  (*((volatile type *)a))
#define FIND_W(val, dataType) FIND_T(FIND(val), dataType) 

typedef struct testing_t {
  union {
    struct {
      uint32_t avr_one :  4;
      uint32_t avr_one :  4;
      uint32_t avr_one :  4;
      uint32_t avr_one :  2;
      uint32_t bcr_one :  4;  
      uint32_t test_one : 4;  
      uint32_t reserved :  1; 
      uint32_t bcr_three : 2; 
      uint32_t reserved1 :  1;
      uint32_t test_one :  3;
      uint32_t avg  : 2;
      uint32_t reserved2 : 1;
    };
    uint32_t VALUE32;
  };
} REQ_ONE;

1 个答案:

答案 0 :(得分:1)

扩大此收益率:

REQ_ONE *reqReg = (*((volatile REQ_ONE *)((void *)0x00652000UL + 0x00000500UL)))

并且有一个*太多,因此您取消引用您指向的内容并希望将其分配给指针类型变量。错误发生在:

#define FIND_T(a, type)  (*((volatile type *)a))    // wrong
#define FIND_T(a, type)  ( ((volatile type *)a))    // no dereference