2016-03-31 53 views
0

我有這個功能在我的程序:無效轉換const的無符號字符

unsigned char * stringXOR(const unsigned char * X, const unsigned char * Y, int len) 
{ 
    unsigned char * result = new unsigned char [1024]; 
    for(int i = 0; i < len; i++) 
    { 
     result[i] = X[i]^Y[i]; 
    } 
    result[len] = '\0'; 
    return result; 
} 
在main()

unsigned char ot1[1024] = "ksjdhsjd"; 
unsigned char ot2[1024] = "jjjkjhyh"; 

unsigned char * computeKey; 
computeKey = stringXOR(ot1, ot2, strlen(ot1)); 

,我得到一個錯誤invalid conversion from ‘unsigned char*’ to ‘const char*’ [-fpermissive]|

什麼是這裏的問題?我沒有返回任何const,所以抱怨什麼?

+1

總是提供錯誤發生的線。 – Downvoter

+0

閱讀如何指定'strlen'。 –

回答

2

strlen預計const char *。您正嘗試將const unsigned char *傳遞給它。這是錯誤信息的內容。

+0

哦,所以我必須每次使用strlen與其他const char * – lllook

+0

@lllook,*除const char *之外的其他東西* - *除了'char以外,不應該使用'strlen' *'或'const char *'。 –

2

錯誤不是關於任何轉換到const,它是關於將指針轉換爲指向(常量)char *的指針unsigned char

相關問題