-9
這就是我想要做的,但我的代碼要麼不編譯,要麼給我一個意外的輸出「BC」而不是「B」。如何在C中通過引用傳遞數組?
#include <stdio.h>
void removeFirstAndLastChar(char** string) {
*string += 1; // Removes the first character
int i = 0;
for (; *string[i] != '\0'; i++);
*string[i - 1] = '\0';
}
int main(void) {
char* title = "ABC";
removeFirstAndLastChar(&title);
printf("%s", title);
// Expected output: B
return 0;
}
我經歷了很多這裏涉及到通過引用傳遞指針答案看了,但他們都不似乎包含我想在我的removeFirstAndLastChar()函數來完成操作。
試圖修改字符串文字的未定義行爲。 – EOF
你應該把'char * title =「ABC」;'改成'char title [] =「ABC」;' – mch
你需要'(* string)[i]'而不是'* string [i]'。 –