意外移動,實乃指針我做了一個字符串連接功能:字符串用C
void
strcat(char *s, char *t)
{
while(*s)
s++;
while(*s++ = *t++);
}
似乎就好了工作:
main()
{
char *s = "Hello, ";
char *t = "world!";
strcat(s,t);
printf("%s\n", s);
return 0;
}
產生Hello, world!
預期。但一些不必要的事情也發生了,打印字符串t
指向orld!
。 strcat
不能改變t
。相反,它似乎是字符串已經移動;在strcat後遞減t
,然後打印它給出正確的字符串。
什麼移動了字符串? strcat
必須是它,但不知道它有什麼問題。
編譯爲tcc版本0.9.26(x86-64 Win64)如果有問題。
請注意,以'str'開始的名字和一個字母總是保留給實現,所以你不應該命名你的新函數。 –