用C语言写一个函数,要求使用switch语句解析typedef struct { unsigned char head1; unsigned char head2; unsigned char len; unsigned char cmdid; unsigned char data[255]; unsigned char checksum; unsigned char tail; } Frame;这个结构体中cmdid
时间: 2023-03-31 18:00:34 浏览: 110
的取值范围是-255,现在需要写一个函数,根据传入的cmdid值返回对应的命令名称,如果cmdid不在-255的范围内,则返回"未知命令"。
以下是示例代码:
```
#include <stdio.h>
typedef struct {
unsigned char head1;
unsigned char head2;
unsigned char len;
unsigned char cmdid;
unsigned char data[255];
unsigned char checksum;
unsigned char tail;
} Frame;
char* get_cmd_name(unsigned char cmdid) {
char* cmd_name;
switch (cmdid) {
case :
cmd_name = "命令";
break;
case 1:
cmd_name = "命令1";
break;
// 其他命令的case语句
case 255:
cmd_name = "命令255";
break;
default:
cmd_name = "未知命令";
break;
}
return cmd_name;
}
int main() {
Frame frame;
frame.cmdid = 10;
printf("命令%d对应的名称是%s\n", frame.cmdid, get_cmd_name(frame.cmdid));
return ;
}
```
阅读全文