我有一個連接兩個常量char *並返回結果的函數。我想,雖然做的是加入一個字符一個常數的char *如如何將char加入常量char *?
char *command = "nest";
char *halloween = join("hallowee", command[0]); //this gives an error
char *join(const char* s1, const char* s2)
{
char* result = malloc(strlen(s1) + strlen(s2) + 1);
if (result)
{
strcpy(result, s1);
strcat(result, s2);
}
return result;
}
好,我明白,我可以改變這個函數需要的參數,但仍,的strcpy( )和strcat()需要canst char * ..而我擁有的是一個簡單的char。所以我需要做一些命令[0],所以我可以使用它與strcpy()和strcat()。謝謝 – user870565
抱歉,沒有刷新我的網頁。讓我看看你編輯 – user870565
謝謝,我想避免改變函數本身,因爲我用它在其他地方加入兩個常量字符*。所有相同的謝謝,我只是爲此目的創建另一個。 :-) – user870565