• 在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;
  • 相关阅读:
    CNCC2017中的深度学习与跨媒体智能
    CNCC2017梳理
    Keras Xception Multi loss 细粒度图像分类
    西瓜书概念整理(chapter 1-2)熟悉机器学习术语
    Google机器学习笔记(七)TF.Learn 手写文字识别
    Google机器学习笔记 4-5-6 分类器
    TensorFlow深度学习笔记 Tensorboard入门
    Ubuntu安装与初始配置
    TensorFlow深度学习笔记 循环神经网络实践
    第10组 Alpha冲刺(6/6)
  • 原文地址:https://www.cnblogs.com/wangluojisuan/p/3420019.html
Copyright © 2020-2023  润新知