【发布时间】:2021-12-25 07:13:40
【问题描述】:
在这个函数中,我试图检查一个 char 数组是否包含广播 MAC 地址。为此,我将每个数组元素与0xFF 进行比较。
static inline bool recibir_trama_l2_en_interface(interface_t *interface, cab_ethernet_t *cab_ethernet) {
char *mac = MAC_IF(interface);
if(IF_EN_MODO_L3(interface)) {
char *mac_destino = cab_ethernet->mac_destino.dir_mac;
mostrar_dir_mac(&interface->prop_intf->dir_mac);
mostrar_dir_mac(&cab_ethernet->mac_destino);
bool bandera_prueba = true;
bandera_prueba = mac_destino[0] == 0xFF;
if(mac_destino[0] == 0xFF && mac_destino[1] == 0xFF && mac_destino[2] == 0xFF && mac_destino[3] == 0xFF && mac_destino[4] == 0xFF && mac_destino[5] == 0xFF) {
printf("Esto está pasando.\n");
}
printf("AABBNINKD.\n");
if(mac_destino[0] == 0xFF) {
printf("Esto sí pasa.\n");
}
}
return false;
}
这些是我正在使用的结构。
typedef struct cab_ethernet_ {
dir_mac_t mac_destino;
dir_mac_t mac_origen;
short tipo;
char payload[TAM_MAX_PAYLOAD];
unsigned int FCS;
} cab_ethernet_t;
typedef struct dir_mac_ {
char dir_mac[TAM_DIR_MAC];
} dir_mac_t;
调试器显示mac_destino[0] 的内容是0xFF。但是你也可以看到,经过比较,bandera_prueba 被设置为false。
正在发生的另一件事是程序显然正在跳过这些指令。
if(mac_destino[0] == 0xFF && mac_destino[1] == 0xFF && mac_destino[2] == 0xFF && mac_destino[3] == 0xFF && mac_destino[4] == 0xFF && mac_destino[5] == 0xFF) {
printf("Esto está pasando.\n");
}
if(mac_destino[0] == 0xFF) {
printf("Esto sí pasa.\n");
}
【问题讨论】:
-
我想你可能希望
struct dir_mac_使用unsigned char类型。