zoukankan      html  css  js  c++  java
  • 在switch中的case语句中声明变量编译出错的解决方案

    在switch中的case语句中声明变量编译的问题

    先来看段代码,别管什么意思:

    case 10: 
    
    int i = 0, j = 0;
    
    for (i = 0; i < 11; i++)
    
    recive_phone[i] = msgbuf.text[i];
    
    recive_phone[i] = '';
    
    printf("%s文件%s函数%d行:接收端号码:%s
    ", __FILE__
    
    , __FUNCTION__, __LINE__, recive_phone);
    
    for (j = 0; msgbuf.text[i] != '' && j < 12; i++,j++)
    
    center_phone[j] = msgbuf.text[i];
    
    center_phone[j] = '';
    
    printf("%s文件%s函数%d行:发送端号码:%s
    ", __FILE__
    
    , __FUNCTION__, __LINE__, center_phone);
    
    break;

    我在case:break中声明了变量,结果gcc编译时就提示:

    error: a label can only be part of a statement and a declaration is not a statement

    有下面三种方法处理:

    1、将变量定义放到case:break外面;

    2、将case:break中间的语句用{}包含;

    case 10: {
    
    int i = 0, j = 0;
    
    for (i = 0; i < 11; i++)
    
    recive_phone[i] = msgbuf.text[i];
    
    recive_phone[i] = '';
    
    printf("%s文件%s函数%d行:接收端号码:%s
    ", __FILE__
    
    , __FUNCTION__, __LINE__, recive_phone);
    
    for (j = 0; msgbuf.text[i] != '' && j < 12; i++,j++)
    
    center_phone[j] = msgbuf.text[i];
    
    center_phone[j] = '';
    
    printf("%s文件%s函数%d行:发送端号码:%s
    ", __FILE__
    
    , __FUNCTION__, __LINE__, center_phone);
    
    }
    
    break;

    注意case后{}括号

    3、在“case:”后面加“;”处理。

    case 10: ;
    
    int i = 0, j = 0;
    
    for (i = 0; i < 11; i++)
    
    recive_phone[i] = msgbuf.text[i];
    
    recive_phone[i] = '';
    
    printf("%s文件%s函数%d行:接收端号码:%s
    ", __FILE__
    
    , __FUNCTION__, __LINE__, recive_phone);
    
    for (j = 0; msgbuf.text[i] != '' && j < 12; i++,j++)
    
    center_phone[j] = msgbuf.text[i];
    
    center_phone[j] = '';
    
    printf("%s文件%s函数%d行:发送端号码:%s
    ", __FILE__
    
    , __FUNCTION__, __LINE__, center_phone);
    
    break;
  • 相关阅读:
    Spring Boot 自定义starter
    jvm中的年轻代 老年代 持久代 gc
    nginx反向代理服务器端口问题
    ACE Editor在线代码编辑器简介及使用引导
    Linux下MySQL 5.6.24的编译安装与部署
    C3p0的参数
    Mysql 查看连接数,状态
    linux下mysql定时备份数据库
    Mysql中存储方式的区别
    mysql常用语句
  • 原文地址:https://www.cnblogs.com/wangluojisuan/p/3420019.html
Copyright © 2011-2022 走看看