我傳遞一個空的字符數組,我需要遞歸使用strcat()
填充。但是,在VS調試器中,數組不是空的,它充滿了我不認識的一些奇怪的垃圾字符。 strcat()然後追加到這些垃圾字符的末尾,而不是在數組的前面。VS2008中的char []問題 - 爲什麼strcat追加到空數組的末尾?
我也嘗試過encoded[0] = '\0'
在傳遞數組之前清除垃圾,但接着strcat()不會在遞歸調用中追加任何東西。
這是供應數組,並調用遞歸函數的代碼:
char encoded[512];
text_to_binary("Some text", encoded);
這是遞歸函數:
void text_to_binary(const char* str, char* encoded)
{
char bintemp[9];
bintemp[0] = '\0';
while(*str != '\0')
{
ascii_to_binary(*str, bintemp);
strcat(encoded, bintemp);
str++;
text_to_binary(str, encoded);
}
}
這是怎麼回事?
ps。我無法使用std::string
- 我堅持使用char*
。
編輯:這是數組中的垃圾字符: ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ...
編碼[0] ='\ 0'應該可以工作,你在調用text_to_binary之前是否做過這個? – 2010-01-07 16:07:55
「junk character」是十六進制的0xcc Debug build(在Visual C++下)使用此值初始化堆棧變量以幫助在調試時顯示未初始化的變量用法ING。 – 2010-01-07 16:21:29