我用strrchr得到一些奇怪的結果。請幫幫我。奇怪的行爲與功能strrchr
這裏是我的代碼:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char szString[32] = "hello-world-and-everyone";
char* ptemp = NULL;
ptemp = strrchr(szString, '-');
if (ptemp == NULL)
{
printf("Invalid string\n");
return 1;
}
printf("%s\n", szString);
printf("%s\n", ptemp);
*ptemp = 0;
printf("%s\n", szString);
return 0;
}
預期結果:
hello-world-and-everyone
-everyone
hello-world-and-everyone
Acutal結果:
hello-world-and-everyone
-everyone
hello-world-and
請參閱'man strrchr' – lurker