c语言编程出错汇总

来源:岁月联盟 编辑:exp 时间:2012-10-09

以下错误实例是本人在编程过程中出现的错误,在此我进行一下总结并提出预防此错误的相关措施 。 

实例1:

错误代码

     ……

for (i=count-1; i>=0; i--)

{
if ((ULong)memblock == (ULong)(pMemInfo[i].addr));
{
fprintf(pf, "nAllSize = %u/n", pMemInfo[count-1].nSizeAll);
pMemInfo[count-1].nSizeAll -= pMemInfo[i].nSize;
break;
}

               ……

         }

         ……

  错误原因:if语句后面多添加了一个分号,导致if语句下面大括号包含的语句每次都执行。

   预防措施:每次写if语句都带上else关键字 ,即if……else……成对出现,这样如果出现以上情况,则编译的时候会出现语法错误,即可以避免此类错误的出现。

    

       改正后代码:

       ……

for (i=count-1; i>=0; i--)

{
if ((ULong)memblock == (ULong)(pMemInfo[i].addr))
{
fprintf(pf, "nAllSize = %u/n", pMemInfo[count-1].nSizeAll);
pMemInfo[count-1].nSizeAll -= pMemInfo[i].nSize;
break;
}

                else

               {

               }

               ……

         }


 

图片内容