【问题标题】:Warning: parameter names (without types) in function declaration警告:函数声明中的参数名称(无类型)
【发布时间】:2011-04-19 07:22:10
【问题描述】:

菜鸟问题仅供参考。

每当我编译/运行代码时,extern tolayer2(rtpktTo1); 我都会收到警告。 警告如标题所示,警告:函数声明中的参数名称(无类型)

任何帮助表示赞赏。

node0.c

extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* current understanding of min cost to node 0 ... 3 */
  };

/* Create routing packets (rtpkt) and send to neighbors via tolayer2(). */
    struct rtpkt rtpktTo1;
        rtpktTo1.sourceid = 0;
        rtpktTo1.destid = 1;
        rtpktTo1.mincost[0] = minCost[0];
        rtpktTo1.mincost[1] = minCost[1];
        rtpktTo1.mincost[2] = minCost[2];
        rtpktTo1.mincost[3] = minCost[3];

extern tolayer2(rtpktTo1);

prog3.c

tolayer2(packet)
  struct rtpkt packet;
{
  /* This has a lot of code in it */ 
}

【问题讨论】:

    标签: c


    【解决方案1】:

    rkpktTo1.* 的赋值显然不在函数或声明中,除非这是一个代码片段。将它们包装在一个函数中。警告有点误导。

    tolayer2() 的声明应该有一个返回类型和一个参数类型。由于没有,因此假定为int。这可能不是预期的,但它应该在没有警告和错误的情况下编译:

    node0.c

    struct rtpkt {
      int sourceid;       /* id of sending router sending this pkt */
      int destid;         /* id of router to which pkt being sent 
                             (must be an immediate neighbor) */
      int mincost[4];    /* current understanding of min cost to node 0 ... 3 */
      };
    
    /* Create routing packets (rtpkt) and send to neighbors via tolayer2(). */
    void function () {
        struct rtpkt rtpktTo1;
            rtpktTo1.sourceid = 0;
            rtpktTo1.destid = 1;
            rtpktTo1.mincost[0] = minCost[0];
            rtpktTo1.mincost[1] = minCost[1];
            rtpktTo1.mincost[2] = minCost[2];
            rtpktTo1.mincost[3] = minCost[3];
    }
    extern void tolayer2(struct rtpkt *rtpktTo1);
    

    prog3.c

    void
    tolayer2(struct rtpkt *packet)
    {
      /* This has a lot of code in it */ 
    }
    

    按值传递结构通常不合适,因此我将其更改为按引用传递。

    【讨论】:

      【解决方案2】:

      在 prog3.c 中

      tolayer2(packet)
        struct rtpkt packet;
      { /* ... */ }
      

      这是旧语法(非常古老:在 1989 年 ANSI 标准化 C 之前),但在 C89 和 C99 中完全合法。但是不要使用它:首选

      int tolayer2(struct rtpkt packet)
      { /* ... */ }
      

      【讨论】:

        【解决方案3】:

        在声明 extern tolayer2(rtpktTo1); 中,rtpktTo1 是一个参数名称(如错误所说),而您应该在那里给出一个类型:

        extern tolayer2(struct rtpkt);
        

        extern tolayer2(struct rtpkt *);
        

        extern tolayer2(struct rtpkt const *);
        

        或类似的,因为这是编译器在编译客户端代码之前需要了解您的函数的内容。此时参数名称对编译器来说是无用的,因此是可选的。

        (实际上,您还应该添加一个返回类型,并注意extern 在您的struct 定义中没有任何意义。)

        【讨论】:

        • 这也很有帮助,谢谢。问题出在外部调用上,我在输入 prog3.c 时犯了一个错误。
        猜你喜欢
        • 2013-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-16
        相关资源
        最近更新 更多