C语言相关Warning。。。解决方案

来源:互联网 发布:网络无武侠小说 编辑:程序博客网 时间:2024/05/18 03:16

1. warning: "     

  说明:意思是说中间又包含了   


2.   warning: no previous prototype for
     'get_char_for_sta'

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

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

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

5. warning:unused variable `iRet'

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

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

7.  warning: declaration of 'remove' shadows aglobal  declaration
  
举例:
      int
   bcm_port_learn_modify(int unit, bcm_port_t port, uint32add, 
                         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
  
linuxpatch中也是采用的修改变量名的方法。
  linux patch

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

9.  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} 
          };

10.   warning: function declaration isn't a   prototype

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

11.  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;
       elseif(nmhGetIssMirrorCtrlIngressMirroring(i4Index,&i4IngressMirroring)
           ==SNMP_SUCCESS)
              if(i4IngressMirroring== ISS_ENABLE)
                 break;
     } 
说明:如果没有缩进,写成这样人都看不懂了,更何况编译器?
修改:重写这段代码 
附:  webdiffserv部分,有一部分代码使用
          if
            ;
           else if
            ;
           ........
          else
            ;
      
其嵌套达到10层以上,令人叹为观止,结果编译出来的代码有问题,产生error.

12.   warning:comparison between signed and  unsigned

举例:int iLen

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

13.  array subscript has type `char'
举例:        I8_T                        i8TrunkSearch;                                                if(i32TrunkID[i8TrunkSearch]==i32CurrentTrunk)

                   ...............;
                   ...............;
说明:这个warning是说,数组的下标被定义成char型了,由于char型有可能是负数,因此会产生难以
      
预料的错误。
      
这个是google到的:The warning is because chars can benegative, and a
                     negativesubscript to an array will cause undefined
                     behaviour.Thisis not a required diagnostic; I guess 
                     theGCC developers feel that this error is more likely
                     tooccur with a char than with other signed integral 
                     types

修改:使用无符号类型替代有符号类型。

14.   warning: `return' with no value, in function
      returning non-void
举例:
     INT1 MSTPGetInstanceVlanMap(UI32_Tu32InstIndex,UINT1 *vlanlist) 
       {
         .................;
         .................;
         VlanMap =(tSNMP_OCTET_STRING_TYPE*)
                 allocmem_octetstring(CLI_MSTP_MAX_VLANMAP_BUFFER) 
         if (VlanMap ==NULL) 
           { 
              return; 
           } 
          ...............;
          ...............;
       }
说明:由于该函数要求返回一个INT1型的数,而这里使用return;所以会产生warning
修改:添加上相应的返回值。

15.    warning: control reaches end ofnon-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;

16.  warning: return type defaults to `int'

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

17.  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型。

18.   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 isit 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 thelast line of your source code and 
      press "Return".

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

0 0
原创粉丝点击