【发布时间】:2018-04-06 15:06:47
【问题描述】:
我的主函数中有我的 switch 案例和语句,如下所示:
int main(int argc, char *argv[])
{
int c;
while((c = getopt(argc,argv,"ABS"))!=-1)
{
switch(c)
{
case 'A':
flag = 0;
printf("open the port\n");
struct can_frame frame_rd;
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_file();
while(1)
{
read_port(&frame_rd);
}
break;
case 'B':
flag = 1;
printf("open the port\n");
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_binfile();
while(1)
{
read_port(&frame_rd);
}
break;
}
}
现在我想在用户在case A 中传递参数 -S 时使用嵌套开关,我可以按以下方式执行吗?下面的步骤正确吗?
int main(int argc, char *argv[])
{
int c;
while((c = getopt(argc,argv,"ABS"))!=-1)
{
switch(c)
{
case 'A':
switch(c)
{
case 'S':
size = 100;
break;
}
flag = 0;
printf("open the port\n");
struct can_frame frame_rd;
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_file();
while(1)
{
read_port(&frame_rd);
}
break;
case 'B':
flag = 1;
printf("open the port\n");
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_binfile();
while(1)
{
read_port(&frame_rd);
}
break;
}
}
在上面的代码中,我也可以将相同的switch(c) 用于嵌套情况吗,嵌套开关的使用是否正确?提前谢谢。
【问题讨论】:
标签: c switch-statement case