消息算術工作:爲什麼沒有在指針的char *功能
/usr/local/webide/runners/c_runner.sh: line 54: 20533 Segmentation fault
nice -n 10 valgrind --leak-check=full --log-file="$valgrindout" "$exefile"
我不明白爲什麼我不能使用指針運算時,我的功能類型不是void。看看這個例子:
假設我必須編寫一個函數,可以在字符串中的第一個單詞之前'擦除'所有空格。 例如,如果我們有一個字符數組:
" Hi everyone"
它應該生產函數的修改後"Hi everyone"
。
這裏是我的代碼的時候,而不是 char* EraseWSbeforethefirstword()
我
void EraseWSbeforethefirstword
工作正常。
當函數返回一個對象char*
它甚至不能被編譯。
char* EraseWSbeforethefirstword(char *s) {
char *p = s, *q = s;
if (*p == ' ') { /*first let's see if I have a string that begins with a space */
while (*p == ' ') {
p++;
} /*moving forward to the first non-space character*/
while (*p!= '\0') {
*q = *p;
p++;
q++;
} /*copying the text*/
*q = '\0'; /*If I had n spaces at the beginning the new string has n characters less */
}
return s;
}
究竟是什麼你想達到什麼樣的?哦,你可以請格式化你的代碼,這樣它可以更容易閱讀...... – ZeusInTexas
你沒有在編譯的代碼中添加return語句嗎? –
你的函數改變's'的位置。看起來你想讓它返回一些東西。你想讓它返回什麼?請向我們展示您的代碼,不編譯。 –