2012-04-10 48 views
0

我想確定我的字符串中的每個字符是否是字母數字。我的編譯器沒有isalnum函數。isalnum等效使用#define

我的函數如下,my_struct有一個大小爲6的字符數組(uint8 bom_pn [6])....並且是uint8是char。

boolean myfunc(my_struct * lh) 
{ 
ret = (isalphanum(lh->bom_pn) && isalphanum(lh->bom_pn + 1) && 
     isalphanum(lh->bom_pn + 2) && isalphanum(lh->bom_pn + 3) && 
     isalphanum(lh->bom_pn + 4) && isalphanum(lh->bom_pn + 5)); 
} 

我的宏定義如下:

#define isalphanum(c) ((c >= '0' && c <= '9') || \ 
         (c >= 'A' && c <= 'Z') || \ 
         (c >= 'a' && c <= 'z')) 

上述引發錯誤 「操作數類型是不兼容的(」 UINT8 * 「和」 INT 「)」

如果我改變我的定義到下面,我的代碼編譯,我得到警告。

#define isalphanum(c) (((uint8)c >= '0' && (uint8)c <= '9') || \ 
        ((uint8)c >= 'A' && (uint8)c <= 'Z') || \ 
        ((uint8)c >= 'a' && (uint8)c <= 'z')) 

警告:「從指針轉換爲更小的整數」

我的問題是,我該如何正確地創建這個定義沒有警告(顯然正確檢查)。

由於

+0

需要注意的是,超出了你的指針指向與 - 問題,你沒有處理區域設置。爲什麼你的環境沒有'ctype.h'? – geekosaur 2012-04-10 18:43:43

+0

銖,我的意思是說回報(isal .....) – NickG 2012-04-10 18:50:27

回答

3

至於你說lh->bom_pn是一個字節數組,這意味着它是一個有效的指針。

所以當你將它傳遞給isalphanum時,你傳遞了一個指針,並將它與字面字節進行比較。

你有兩個選擇:

1)

ret = (isalphanum(lh->bom_pn[0]) && isalphanum(lh->bom_pn[1]) && 
     isalphanum(lh->bom_pn[2]) && isalphanum(lh->bom_pn[3]) && 
     isalphanum(lh->bom_pn[4]) && isalphanum(lh->bom_pn[5])); 

2)

#define isalphanum(c) ((*(c) >= '0' && *(c) <= '9') || \ 
         (*(c) >= 'A' && *(c) <= 'Z') || \ 
         (*(c) >= 'a' && *(c) <= 'z')) 

無論是一個應該解決您的問題。

+0

感謝您的回覆和詳細信息。有趣的是,如何從三個用戶那裏得到三種不同的解決方案:) – NickG 2012-04-10 18:52:43

+0

again * *(c)> ='A'&& *(c)<='Z''如果字符集是ASCII但在一般情況下不是OK案例(例如,EBCDIC) – ouah 2012-04-10 18:58:50

1

變化

lh->bom_pn+i //pointer 

所有出現

lh->bom_pn[i] //character 
+0

解決它的一種方法。謝謝 – NickG 2012-04-10 18:51:55

+0

您的歡迎:) – 2012-04-10 18:52:07

1

由於bom_pn是你需要把它作爲isalphanum(*lh->bom_pn)數組,isalphanum(*lh->bom_pn+i)

+0

...另一種方法來解決它。謝謝 – NickG 2012-04-10 18:52:06