【问题标题】:return from incompatible pointer type warning in C从 C 中的不兼容指针类型警告返回
【发布时间】:2013-01-26 04:34:35
【问题描述】:

我对C不是很熟悉,当我尝试使用C构建一个链表时,我遇到了一个小问题,希望有人为我澄清一下。

到目前为止,这是我的代码:

typedef struct {
struct dlinkNode_t *next;
struct dlinkNode_t *prev;
void *value;
} dlinkNode_t;

dlinkNode_t* getNext(dlinkNode_t *ptr){
/**return the next node of the node pointed by ptr. Return NULL if next element*/
return ptr->next;

当我尝试编译它时,我收到以下警告:

"warning: return from incompatible pointer type"

我将 dlinkNode_t 定义为我的链表节点的类型,每个节点有 2 个指向前后的指针。我应该将 getNext 的返回类型定义为:

struct dlinkNode_t*

但这似乎违背了typedef的目的,因为我想将dlinkNode定义为一个新的类型。任何帮助都会很好。

谢谢!

【问题讨论】:

    标签: c linked-list


    【解决方案1】:

    希望这有帮助.. struct 必须有名称,当您在其中使用相同的 struct 指针时......

    typedef struct dlinkNode {
    struct dlinkNode *next;
    struct dlinkNode *prev;
    void *value;
    } dlinkNode_t;
    
    dlinkNode_t* getNext(dlinkNode_t *ptr){
    /**return the next node of the node pointed by ptr. Return NULL if next element*/
    return ptr->next;
    

    【讨论】:

      【解决方案2】:

      改成

      typedef struct dlinkNode_t {
          struct dlinkNode_t *next;
          struct dlinkNode_t *prev;
          void *value;
      } dlinkNode_t;
      

      【讨论】:

        猜你喜欢
        • 2012-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多