天天看點

c 語言 uint8 轉char,在C中的uint8和char之間的轉換

我有一個API來實作對EEPROM的寫入操作。下面是它的聲明:在C中的uint8和char之間的轉換

CYBLE_API_RESULT_T CyBle_StoreAppData (uint8 * srcBuff, const uint8 destAddr[], uint32 buffLen, uint8 isForceWrite);

它運作良好時,我調用此函數,并發送一個陣列參數已被宣布為uint8型srcBuff。

問題是,我需要發送char數組指針。我在想char已經是uint8,但是如果我發送一個char數組指針而不是uint8,我會得到一個編譯器警告。為什麼我不能使用char而不是uint8?下面是調用該函數的2個例子:

static const uint8 datastack_ROM[dedicatedRomSize] = {0};

uint8 Container_ID[10];

char Prefix[10];

//Call the function with Container_ID which has been declared as uint8. This is working.

CyBle_StoreAppData(Container_ID,datastack_ROM,10,0);

//Call the function with Prefix which has been declared as char. This is NOT working.

CyBle_StoreAppData(Prefix,datastack_ROM,10,0);

下面是第二個電話警告:

passing char[10] to parameter of type 'uint8 *' converts between pointers to integer types with different sign.

是不是char和uint8一樣嗎?

+1

錯誤消息中的重要内容是關于“不同符号”的部分。這意味着你的'char'類型是'signed',而'uint8'是(假設在這裡)'unsigned'。這可能不會是一個大問題,是以你應該隻能夠投射你的指針:'CyBle_StoreAppData((uint8 *)Prefix,...)' –

+0

這似乎違反了限制條件,請注意:http:// stackoverflow。com/questions/30535814/pass-unsigned-char-pointer-to-atoi-without-cast –

+0

@GiorgiMoniava我看過類似的問題,但我想我找不到那個。我怎麼能提到有類似的問題呢?還是我需要那樣做? –