0

http://www.cplusplus.com/reference/cstring/strchr/の strchr() の例を見ています。

これでインデックスが正しく検出されるのはなぜですか? 直感的には、負のインデックスを与える必要があるように見えます。

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}
4

1 に答える 1