在此函式中,我試圖檢查字符陣列是否包含廣播 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");
}
我的意思是,除錯器從第 78 行跳到第 83 行再到第 89 行。
這種比較是否有什么問題導致了這些錯誤?
uj5u.com熱心網友回復:
常量的0xFF值為 255。在您的 C 實作中,char是有符號的,并且只能具有值 -128 到 127。mac_destino[0]是一個char。因此mac_destino[0] == 0xFF永遠不可能是真的。在除錯器中單步執行代碼似乎會跳過幾行,因為編譯器已優化程式以省略不可能的部分。
要解決此問題,請將使用的型別更改為unsigned char.
優選地,改變的元件型別dir_mac中struct dir_mac_,以unsigned char和的型別mac_destino來unsigned char *。如果您不能這樣做,請將mac_destinofrom的定義更改char *mac_destino = cab_ethernet->mac_destino.dir_mac;為unsigned char *mac_destino = (unsigned char *) cab_ethernet->mac_destino.dir_mac;。
如果您不能這樣做,您可以在每個比較中插入一個轉換,如更改mac_destino[0] == 0xFF為(unsigned char) mac_destino[0] == 0xFF.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358589.html
