2012-02-07 116 views
-1

我想知道关于C++中的strchr函数。需要了解C++ strchr函数及其工作原理

例如:

realm=strchr(name,'@'); 

什么是此行的意义?

+5

甚至不想想谷歌搜索里输入'strchr' ... – 2012-02-07 10:52:52

+5

谁顶起这个?!哇。 – cnicutar 2012-02-07 10:53:08

+2

您已经咨询了[docs](http://linux.die.net/man/3/strchr),对不对? – 2012-02-07 10:53:35

回答

2

here

返回一个指向C字符串str中第一个出现的字符的指针。

终止空字符被认为是C字符串的一部分。因此,它也可以位于检索指向字符串结尾的指针。

/* strchr example */ 
#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; 
} 

会产生输出

Looking for the 's' character in "This is a sample string"... 
found at 4 
found at 7 
found at 11 
found at 18 
2

www.cplusplus.com是C++帮助一个非常有用的网站。如解释功能。

对于strchr

找到字符串字符的第一次出现将指针返回到 字符的在C字符串str第一次出现。

终止空字符被认为是C字符串的一部分。 因此,它也可以被定位来检索指向 结尾的指针字符串。

char* name = "[email protected]"; 
char* realm = strchr(name,'@'); 

//realm will point to "@hello.com" 
0

只为那些谁正在寻找此源代码/实施:

char *strchr(const char *s, int c) 
{ 
    while (*s != (char)c) 
     if (!*s++) 
      return 0; 
    return (char *)s; 
} 

(来源:http://clc-wiki.net/wiki/strchr