switch函数用法与错误分析

1.switch参数类型:
switch 后面的表达式不能跟double,float,long,String ,boolean,可以接int,short,byte,char!

2.switch中定义变量问题:
  问题点:
  switch   (a)   
  {   
        case   1:   
                CString   str="ABCDE";     //这句编译有错误。请问为什么?   
                break;   
        case   2:   
                break;   
        ....   
  }
   
  回复1:
  switch   (a)   
  {   
        case   1:   
                {   
                      CString   str="ABCDE";     //OK  
                }   
                break;   
        case   2:   
                break;   
        ....   
  }   
    
  加上花括号就没事了。这是因为如果a为2的话变量初始化语句将不被执行,  

  回复2:
  switch   是一个单独的程序段。而case(不加{})就不是一个   
  单独的程序段。看这个例子:   
  int   j=1;   
  switch(j)   {   
  case   1:int   b;break;   
  case   2:b=1;break;   
  }   
  可以编译。MSDN有下面的说明:   
  Compiler   Error   C2360   
  initialization   of   'identifier'   is   skipped   by   'case'   label   
    
  The   specified   identifier   initialization   can   be   skipped   in   a   switch   statement.   
    
  It   is   illegal   to   jump   past   a   declaration   with   an   initializer   unless   the   declaration   is   enclosed   in   a   block.   
    
  The   scope   of   the   initialized   variable   lasts   until   the   end   of   the   switch   statement   unless   it   is   declared   in   an   enclosed   block   within   the   switch   statement.   
    
  The   following   is   an   example   of   this   error:   
    
  void   func(   void   )   
  {   
        int   x;   
        switch   (   x   )   
        {   
        case   0   :   
              int   i   =   1;               //   error,   skipped   by   case   1       
              {   int   j   =   1;   }       //   OK,   initialized   in   enclosing   block   
        case   1   :   
              int   k   =   1;               //   OK,   initialization   not   skipped   
        }   
  }   
 
  回复3:
  对于   switch   (k)   
  {   
    case   1:   
              int   i;   
    case   2:     
              i=0;   
  }   
  如果按照whjpn   (常盘平)   的想法,那么会有问题发生:   
      如果   k=1,则没什么   
      如果   k=2,   则i是谁定义的?这个问题怎么解决?   
    
  所以对上述问题的修正办法是:   
      1.在switch外声明变量,这样case   2就不会有错误了   
      2.在switch中声明局部变量,局部变量的标志是用{}   
          即:case   1:   
                        {   int   i;   }   
          但是case   2就不能使用i了 

  另外
    int k = 2;
    switch(k)
    {
        case 0:
            int i;//OK
            int j = 1;//Error
            break;
        case 1:
            {
                int i = 1;
            }
            break;
        case 2:
            i = 2; //OK
            diag_printf("kkkkkkkkkkkkkkkkkk:i=%d\n",i);
        default: ;
    }
   在编译时,case 0的i被当作switch内的变量,所以尽管k=2,i仍然被赋值为2

   即switch变量不能够在case中声明并赋值


转自 http://dennymo.blog.hexun.com/37733543_d.html




  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值