a declaration cannot have a label

本文探讨了在switch语句的case标签后直接声明变量所导致的错误:编译器将其视为标签而非代码的一部分,因此会导致初始化被跳过。文章提供了正确的做法是在case后使用花括号{}

       在使用switch时 在case 后面申请变量会出现 error: a declaration cannot have a label错误

    原因:Case statements are only 'labels'. This means the compiler will interpret this as a jump directly to the label.The problem here is one of scope. Your curly brackets define the scope as everything inside the 'switch' statement. This means that you are left with a scope where a jump will be performed further into the code skipping the initialization. The correct way to handle this is to define a scope specific to that case statement and define your variable within it.

       解决方法:在case 后面加一个{ }做相应的操作。

*** Using Compiler 'V5.06 update 4 (build 422)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' Rebuild target 'Main' compiling cfg_value.c... compiling conn.c... ..\conn.c(20): warning: #177-D: variable "can_box" was declared but never referenced CAN_FIFOMailBox_TypeDef *can_box; ..\conn.c: 1 warning, 0 errors compiling IO.c... compiling stm32f10x_it.c... compiling updata.c... ..\updata.c(78): warning: #177-D: variable "add" was declared but never referenced uint32_t add,file_flag; ..\updata.c(78): warning: #177-D: variable "file_flag" was declared but never referenced uint32_t add,file_flag; ..\updata.c: 2 warnings, 0 errors compiling main.c... ..\main.c(87): warning: #1296-D: extended constant initialiser used {0,(u32)motor_step_cmd_demo}, ..\main.c(88): warning: #1296-D: extended constant initialiser used {0,(u32)cfg_write_buf}, ..\main.c(94): warning: #177-D: variable "status" was declared but never referenced static u8 status=0; ..\main.c: 3 warnings, 0 errors compiling HardWare.c... ..\HardWare.c(162): warning: #188-D: enumerated type mixed with another type GPIO_WriteBit( TEP_CFG2_GPIO, TEP_CFG2_PIN,val&1); ..\HardWare.c(163): warning: #188-D: enumerated type mixed with another type GPIO_WriteBit( TEP_CFG3_GPIO, TEP_CFG3_PIN,(val>>1)&1); ..\HardWare.c(164): warning: #188-D: enumerated type mixed with another type GPIO_WriteBit( TEP_CFG4_GPIO, TEP_CFG4_PIN,(val>>1)&1); ..\HardWare.c(501): warning: #111-D: statement is unreachable if ((CAN1->TSR&CAN_TSR_TME2) != CAN_TSR_TME2) ..\HardWare.c: 4 warnings, 0 errors compiling stm32f10x_crc.c... compiling stm32f10x_flash.c... compiling stm32f10x_rcc.c... compiling stm32f10x_gpio.c... compiling motor.c... ..\motor.c(487): warning: #177-D: variable "i" was declared but never referenced uint32_t i,time_run; ..\motor.c(488): warning: #177-D: variable "ptr16" was declared but never referenced uint16_t *ptr16,ad_val; //最大4096 *10 不会超过16位 ..\motor.c(855): warning: #177-D: variable "i" was declared but never referenced uint32_t i,time_run; ..\motor.c(1493): error: #1182: a declaration cannot have a label long pulses; ..\motor.c(1494): error: #268: declaration may not appear after executable statement in block uint32_t calc_temp ; ..\motor.c(1495): error: #268: declaration may not appear after executable statement in block uint32_t cur_mm10 ; ..\motor.c: 3 warnings, 3 errors compiling stm32f10x_can.c... compiling misc.c... compiling stm32f10x_dma.c... compiling stm32f10x_usart.c... compiling stm32f10x_spi.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_adc.c... compiling stm32f10x_tim.c... compiling stm32f10x_iwdg.c... compiling system_stm32f10x.c... assembling startup_stm32f10x_md.s... compiling core_cm3.c... ".\obj\ir_12.axf" - 3 Error(s), 13 Warning(s). Target not created. Build Time Elapsed: 00:00:20 解读一下报错信息
最新发布
09-02
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值