2012-02-07 96 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