typedef enum 数据类型,别名a,内容有周一到周日。定义 a *b,怎么通过b来判断状态是周一还是周几
时间: 2024-03-18 18:40:12 浏览: 70
用typedef定义类型的总结分析
假设我们已经用 `typedef enum` 定义了名为 `Weekday` 的枚举类型,其中包含了周一到周日的所有枚举值,如下所示:
```c
typedef enum {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
} Weekday;
```
现在,我们可以通过定义一个 `Weekday` 类型的指针 `b`,来指向一个 `Weekday` 枚举值的地址。如下所示:
```c
Weekday *b;
```
要判断 `b` 所指向的枚举值是周几,可以使用 `switch` 语句来进行判断。例如:
```c
switch (*b) {
case MONDAY:
printf("Today is MONDAY.\n");
break;
case TUESDAY:
printf("Today is TUESDAY.\n");
break;
case WEDNESDAY:
printf("Today is WEDNESDAY.\n");
break;
case THURSDAY:
printf("Today is THURSDAY.\n");
break;
case FRIDAY:
printf("Today is FRIDAY.\n");
break;
case SATURDAY:
printf("Today is SATURDAY.\n");
break;
case SUNDAY:
printf("Today is SUNDAY.\n");
break;
default:
printf("Invalid input.\n");
break;
}
```
这样,根据 `b` 所指向的枚举值,就可以判断是周一到周日中的哪一天了。
阅读全文