strchr
在使用 char 指针找到第一次出现后,我试图再获得 2 个字符。该字符串可能如下所示:
foo;bar;2012 -> should output foo;b
foo;caz; -> should output foo;c
foo; -> should output foo (there are no +2 chars)
foo -> null
对于第一种情况,我想我可以做类似的事情,
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "foo;bar;2012";
char *pch = NULL;
if ((pch=strchr(str,';')) != NULL) {
*pch++;
*pch++;
*pch = '\0';
}
puts(str);
return 0;
}
但是检查我是否可以在字符串中推进指针而不越过的正确方法是什么?