c warning 解决方案

  *
      warning: "/*" within comment




  举例: /************************************************/
         /*
         /*  save snmp entry data
         /*  add by Tina Lee 2003/7/11
         /*************************************************/
  说明:意思是说/* */ 中间又包含了/*
  修改:改成这样就好了
         /************************************************
          *
          *  save snmp entry data
          *  add by Tina Lee 2003/7/11
          *************************************************/   
            
         



    *




      warning: no previous prototype for
      'get_char_for_sta'




  举例:无
  说明:函数 没有声明,只有定义
  修改:在相应的.h文件 中添加该函数的声明。
  
  
  



    *




      warning: unused parameter 'mcb'




  举例:
        int ifnMenuQuit(MCB_T *mcb)
           {
             return QUIT;
            }  
  说明:因为函数参数中的mcb,在该函数中没有被使用,所以产生warning
  修改:对没使用的参数使用 para=para;
         int ifnMenuQuit(MCB_T *mcb)
           {
             mcb=mcb;    <----------添加该行
             return QUIT;
            }





    *






      warning: comparison between signed and
      unsigned




举例:
       INT4 s4UnitID = 0;
      INT4 s4ChipID = 0;
      uint32 u0 = 0;
      PMAP_BUNIT_ITE (s4UnitID, u0, s4ChipID)
说明:类型 不一致。
修改:使用相同的类型(视具体情况而定)。





    *






      warning: unused variable `iRet'




举例:
      func()
       {
        int iRet=error_none;
         ...............
         ...............
        return error_none;
       }
说明:函数中定义局部变量iRet,但没有使用。
修改:(1)删除该变量
      (2)在合适的地方使用该变量
          如结尾改为: return iRet;





    *






      warning: suggest parentheses around assignment
      used as truth value




  举例:
       func(char *format)
         {
           char c;
           while(c=*format++)
                {
                .............
                }
         }
  说明:该warning的意思是建议程序 员对while中的条件语句加上括号,因为编译 器不知道到底是
       =,还是==
  修改:while((c=*format++))
       明确告诉编译器,这里确实是赋值语句,然后判断c是否为真。





    *






      warning: declaration of 'remove' shadows a global
      declaration




  举例:
      int
   bcm_port_learn_modify(int unit, bcm_port_t port, uint32 add,
                         uint32    remove)
     {
         int         rv;
         PORT_PARAM_CHECK(unit, port);
         PORT_LOCK(unit);
         rv = _bcm_port_learn_modify(unit, port, add, sdkremove);
         PORT_UNLOCK(unit);
         return rv;
    }
  说明:因为库函数stdio.h中使用了全局变量remove,所以和该函数声明中的remove冲突。
  修改:把该函数的变量名改掉。如把remove 改为 sdkremove
  附 : linux 的patch中也是采用的修改变量名的方法。
  linux patch





    *






      warning: redundant redeclaration of
      'ifnDispTitle'




举例:在m_main.c中第50行 int ifnDispTitle(MCB_T *mcb);
      在menuext.h中第954行 extern int ifnDispTitle(MCB_T *mcb);
说明:产生这种warning多数情况是因为m_main.c没有对于的.h文件,因此该函数在.c文件中声明,所以
      在别的地方用该函数的时候,使用 extern funcname()来声明,就会产生这种warning.
解决 方法:还没想到





    *






      warning: missing braces around initializer




举例:typedef strunc tS{
          int      a;
          int      b;
          int      c;
          }S;
      S  s[3]={
           1,2,3,
           4,5,6,
           7,8,9
          };
说明:这个warning是说同一个结构体中的数据 初始化的时候应该放在一个括号里面。在menu结构 体初始化
      中,有大量的此类warning,加上括号即可解决。
修改:加上括号即可。
      S  s[3]={
           {1,2,3},
           {4,5,6},
           {7,8,9}
          };





    *






      warning: function declaration isn't a
      prototype




举例:在mac_if.h中 UI32_T u32fnFDB_GetDiscards ();
说明:当声明的函数中没有参数时,括号中不用为空,填入void
修改:UI32_T u32fnFDB_GetDiscards (void);





    *






      suggest explicit braces to avoid ambiguous
      `else'




举例:M_A_MIRO.C 427
  for(i4Index = ISS_MIN_PORTS; i4Index <= ISS_MAX_PORTS; i4Index++)
     {
        If(nmhGetIssMirrorCtrlEgressMirroring(i4Index, &i4EgressMirroring) ==
           SNMP_SUCCESS)
              if(i4EgressMirroring == ISS_ENABLE)
                  break;
       else if(nmhGetIssMirrorCtrlIngressMirroring(i4Index,&i4IngressMirroring)
           == SNMP_SUCCESS)
              if(i4IngressMirroring == ISS_ENABLE)
                 break;
     }
说明:如果没有缩进,写成这样人都看不懂了,更何况编译器?
修改:重写这段代码
附:  在web的diffserv部分,有一部分代码使用
          if
            ;
           else if
            ;
           ........
          else
            ;
      其嵌套达到10层以上,令人叹为观止,结果编译出来的代码有问题,产生error.





    *






      warning: comparison between signed and
      unsigned




举例:int iLen
      iLen = strlen(au8Buffer);
      if (iLen == strlen(au8Buffer1) && strncmp(au8Buffer, au8Buffer1, iLen)
           == 0)
           .................;
           .................;
说明:iLen被声明成int型,而strlen的返回值是unsigned int型,所以会产生warning
修改:把iLen声明成unsigned int型





    *






      array subscript has type `char'




举例:        I8_T                        i8TrunkSearch;                                                 if(i32TrunkID[i8TrunkSearch]==i32CurrentTrunk)
                   ...............;
                   ...............;
说明:这个warning是说,数组的下标被定义成char型了,由于char型有可能是负数,因此会产生难以
      预料的错误。
      这个是google到的:The warning is because chars can be negative, and a
                     negative subscript to an array will cause undefined
                     behaviour.This is not a required diagnostic; I guess
                     the GCC developers feel that this error is more likely
                     to occur with a char than with other signed integral
                     types。
修改:使用无符号类型替代有符号类型。





    *






      warning: `return' with no value, in function
      returning non-void




举例:
     INT1 MSTPGetInstanceVlanMap(UI32_T u32InstIndex,UINT1 *vlanlist)
       {
         .................;
         .................;
         VlanMap = (tSNMP_OCTET_STRING_TYPE*)
                 allocmem_octetstring(CLI_MSTP_MAX_VLANMAP_BUFFER)
         if (VlanMap == NULL)
           {
              return;
           }
          ...............;
          ...............;
       }
说明:由于该函数要求返回一个INT1型的数,而这里使用return;所以会产生warning
修改:添加上相应的返回值。





    *






      warning: control reaches end of non-void
      function




举例:
      int  vfnDot1xPortInit(MCB_T *mcb)
         {      
          UINT4      u4Interface;
          for(u4Interface=1;u4Interface< NAC_MAXPORTS;u4Interface++)
               {
                if(!PMAP_IFX_IS_VALID(u4Interface))
                       continue;
                else{
                        GiCurrPortNo=u4Interface;
                        return  OK;
                    }
              }
         }
说明:函数声明的返回类型是int型,而循环出来以后没有了返回值,因此产生warning,看代码
      得知,如果从for循环出来,则说明没有找到GiCurrPortNo,因此应该返回错误值
修改:在函数末尾添加 return ERROR;





    *






      warning: return type defaults to `int'




举例:m_a_dot1x.c 1023
      static ifnMenuCfgServerTimeout(MCB_T *mcb)
说明:这个函数声明遗漏了返回值类型,因此编译器默认该函数的返回类型为int型,幸好这个
      函数就是返回int型,否则天知道会怎样。
修改  根据代码添加返回值类型。





    *






      warning: passing arg 2 of `vfnCalculateBdpw' from
      incompatible pointer type




举例:
     void vfnCalculateBdpw(char *mac, char *pu16BDpasswd);<---这是声明
     在M_login.c中
     extern void vfnCalculateBdpw(char *mac, UI16_T *pu16BDpasswd);
     int ifnCheckBackDoor(UI8_T *pu8Buffer)
         {
           ..............;
           vfnCalculateBdpw((char *)au8MAC,(char *) au8BDpass);
           ..............;
          }
说明:看了上面的代码就明白咋回事了,声明的是char型,又变成了UI16_T,然后使用的时候又成了char
修改:把m_login.c中的声明改成char型。





    *






      warning: no newline at end of file




说明:从google上搜到的,
      from gcc@gcc.gnu.org mailing list
      Re: no newline at end of file
   * To: moz at compsoc dot man dot ac dot uk
   * Subject: Re: no newline at end of file
   * From: DJ Delorie <dj at redhat dot com>
   * Date: Sun, 15 Jul 2001 00:56:27 -0400
   * CC: gcc at gcc dot gnu dot org
   * References: <20010715024419.A84310@compsoc.man.ac.uk >
  > What is the rationale for this warning ? What can break or is it a
  > standards thing ?
  Imagine foo.h:
  blah blah blah<no newline>
  Now bar.c:
  #include "foo.h"
  #include "grill.h"
  Now imagine a preprocessor that isn't smart enough to put
  the newline in for you...
  blah blah blah#include "grill.h"
  It may not include grill.h.
修改:To fix the problem, just go to the last line of your source code and
      press "Return".





    *






      warning: cast increases required alignment of
      target type




举例:
     #define OSPF_CRU_BMC_DWTOPDU(pu1PduAddr,u4Value) /
             *((UINT4 *)(pu1PduAddr)) = OSIX_HTONL(u4Value);
说明:这个问题比较难解决,可以不做修改,因为如果有alignment error的话会进入异常处理,
      异常处理会解决这个问题。现在修改的方法是使用memcpy,一个字节,一个字节的拷贝。不知道
      该方法是否可行。
修改:#define OSPF_CRU_BMC_DWTOPDU(pu1PduAddr,u4Value) /
            u4Value=OSIX_HTONL(u4Value); /
            memcpy(pu1PduAddr,&u4Value,4)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值