共找到2條詞條名為strlen的結果 展開
- C/C++語言函數
- php語言函數
strlen
C/C++語言函數
strlen所作的僅僅是一個計數器的工作,它從內存的某個位置(可以是字元串開頭,中間某個位置,甚至是某個不確定的內存區域)開始掃描,直到碰到第一個字元串結束符'\0'為止,然後返回計數器值(長度不包含'\0')。
1 | extern unsigned int strlen(char *s); |
在Visual C++ 6.0或Dev-C++中,原型為
1 | size_t strlen(const char *string); |
,其中size_t實際上是unsigned int,在VC6.0或Dev-C++中可以看到這樣的代碼:
1 | typedef unsigned int size_t; |
頭文件:string.h或cstring
格式:strlen (字元指針表達式)
功能:計算給定字元串的(unsigned int型)長度,不包括'\0'在內
說明:返回s的長度,不包括結束符NULL。
相關函數:
1 | TCHAR.H routine _UNICODE & _MBCS not defined_MBCS defined_UNICODE defined_tcslen |
strlen | strlen | wcslen |
_tcsclen | strlen | _mbslen |
舉例1:(在 Dev-C++ 5.11中運行通過)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //#include #include #include #include using namespace std; int main(void) { ios::sync_with_stdio(false); char s[10000]="Hello, World!"; cout << s << "has" << strlen(s) << "character(s)." << endl; //printf("%s has %d character(s).",s,strlen(s)); //getchar(); return 0; } |
運行結果為:
1 | Hello, World! has 13 character(s). |
strlen(char*)函數求的是字元串的實際長度,它求得方法是從開始到遇到第一個'\0',如果你只定義了一個字元型數組但沒有給它賦初值,這個結果是不定的,它會從aa首地址一直找下去,直到遇到'\0'停止。
樣例1
1 2 3 4 5 6 7 8 9 10 | #include #include using namespace std; int main(void) { char aa[100000]; cout< return 0; } //這個樣例的結果是不定的 |
樣例2
1 2 3 4 5 6 7 8 9 10 11 | #include #include using namespace std; int main(void) { char aa[200]={'\0'}; cout< return 0; } |
樣例3
1 2 3 4 5 6 7 8 9 10 | #include #include using namespace std; int main(void) { char aa[200]="Hello, World!"; cout< return 0; } |
樣例4
1 2 3 4 5 6 7 8 9 10 | #include #include using namespace std; int main(void) { char aa[200]="hello"; cout << strlen(aa) << endl; return 0; } //這個樣例的結果為5 |
1 | sizeof(aa); //返回200 |
1 2 3 | int a[10]; sizeof(a) //返回40 (根據語言int型 c/c++由編譯器決定是兩個或四個 java 是四個) |
⒈sizeof操作符的結果類型是size_t,它在頭文件中typedef為unsigned int類型。
該類型保證能容納實現所建立的最大對象的位元組大小。
⒉sizeof是取位元組運算符(關鍵字),strlen是函數。
⒊sizeof可以用類型做參數,strlen只能用char*做參數,且必須是以'\0'結尾的。
sizeof還可以用函數做參數,比如:
1 2 3 4 5 6 7 8 9 10 11 | #include #include using namespace std; int main(void) { short f(); //printf("%d\n",sizeof(f())); cout << sizeof(f()) << endl; return 0; } //輸出的結果是sizeof(short),即2。 |
⒋數組做sizeof的參數不退化,傳遞給strlen就退化為指針了。
⒌大部分編譯程序 在編譯的時候就把sizeof計算過了是類型或是變數的長度。這就是sizeof(x)可以用來定義數組維數的原因
1 2 3 | char str[20000]="0123456789"; long a=strlen(str); //a=10; int b=sizeof(str); //而b=20000; |
6.strlen的結果要在運行的時候才能計算出來,是用來計算字元串的長度,不是類型占內存的大小。
7.sizeof后如果是類型必須加括弧,如果是變數名可以不加括弧。這是因為sizeof是個操作符不是個函數。
⒏當適用了於一個結構類型時或變數, sizeof 返回實際的大小,
當適用一靜態地空間數組, sizeof 歸還全部數組的尺寸。
⒐數組作為參數傳給函數時傳的是指針而不是數組,傳遞的是數組的首地址,
如:
1 2 | fun(char [8]) fun(char []) |
1 2 | //都等價於 fun(char *) |
在C++里參數傳遞數組永遠都是傳遞指向數組首元素的指針,編譯器不知道數組的大小
如果想在函數內知道數組的大小,需要這樣做:
進入函數後用memcpy拷貝出來,長度由另一個形參傳進去
1 2 3 4 5 | fun(unsiged char *p1,int len) { unsigned char* buf = new unsigned char[len+1]; memcpy(buf,p1,len); } |
我們能常在用到 sizeof 和 strlen 的時候,通常是計算字元串數組的長度
看了上面的詳細解釋,發現兩者的使用還是有區別的,從這個例子可以看得很清楚:
1 2 3 | char str[20000]="0123456789"; int a=strlen(str); //a=10; >>>> strlen 計算字元串的長度,以結束符 0x00 為字元串結束。 int b=sizeof(str); //而b=20000; >>>> sizeof 計算的則是分配的數組 str[20000] 所佔的內存空間的大小,不受裡面存儲的內容改變。 |
● 上面是對靜態數組處理的結果,如果是對指針,結果就不一樣了
1 2 3 4 5 6 | char* ss = "0123456789"; sizeof(ss) //結果 4>>>>ss是指向字元串常量的字元指針,sizeof 獲得的是一個指針的值所佔的空間,是char*類型,所以是4 sizeof(*ss) //結果 1>>>> *ss是第一個字元 其實就是獲得了字元串的第一位'0' 所佔的內存空間,是char類型的,佔了 1 位 strlen(ss)= 10 //>>>> 如果要獲得這個字元串的長度,則一定要使用 strlen //sizeof返回對象所佔用的位元組大小. 正確 //strlen返回字元個數. 正確 |
● 在使用strlen時,有一個很特別的情況,就是數組名到指針蛻變
1 2 3 4 5 | char Array[2000] = {'0'}; sizeof(Array) == 2000; char *p = Array; strlen(p) == 1;//sizeof(p)結果為4 //在傳遞一個數組名到一個函數中時,它會完全退化為一個指針 |
———————————————————————————————————————————————
看完以上你是否很清楚sizeof和strlen的區別了呢?還不明白的話,我們看下面幾個例子:
第一個例子
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include #include using namespace std; int main(void) { char* ss = "0123456789"; cout << sizeof(ss) << endl; //結果 4 ===》ss是指向字元串常量的字元指針 cout << sizeof(*ss) << endl; //結果 1 ===》*ss是第一個字元 //大部分編譯程序 在編譯的時候就把sizeof計算過了 是類型或是變數的長度 //這就是sizeof(x)可以用來定義數組維數的原因 return 0; } |
1 2 3 4 5 6 7 8 9 10 11 | #include #include using namespace std; int main(void) { char ss[] = "0123456789"; cout << sizeof(ss) << endl; //結果 11 ===》ss是數組,計算到\0位置,因此是10+1 cout << sizeof(*ss) << endl; //結果 1 ===》*ss 是第一個字元 return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include #include using namespace std; int main(void) { char q[]="abc"; char p[]="a\n"; cout << sizeof(q) << endl; cout << sizeof(p) << endl; cout << strlen(q) << endl; cout << strlen(p) << endl; return 0; } //結果是 4 3 3 2 |
第二個例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include #include using namespace std; class X { int i; int j; char k; }; X x; int main(void) { cout< cout< return 0; } |
第三個例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include #include using namespace std; struct O { int a,b,c,d,e,f,g,h; }; int main(void) { char szPath[MAX_PATH] char const * static_string = "Hello"; cout << sizeof(static_string) << endl; //是 sizeof 一個指針,所以在 32bit system 是 4 char stack_string[] = "Hello"; cout << sizeof(stack_string) << endl; //是 sizeof 一個數組,所以是 sizeof(char[6]) char * string = new char[6]; strncpy(string,"Hello",6"); cout << sizeof(string) << endl; //是 sizeof 一個指針,所以還是 4。和第一個不同的是,這個指針指向了動態存儲區而不是靜態存儲區。 //不管指針指向的內容在什麼地方,sizeof 得到的都是指針的大小 system("PAUSE"); return 0; } |
自定義函數實現strlen() 函數的功能
下面幾種實現strlen函數的源代碼大家參考
例1
1 2 3 4 5 6 7 8 | #include #include typedef unsigned int u_int; u_int Mystrlen(const char*str){ u_int i; assert(str!=NULL); for(i=0;str[i]!='\0';i++); return i; } |
例2
1 2 3 4 5 6 | unsigned int strlen(const char*str){ assert(str!=NULL);unsigned int len=0; while((*str++)!='\0') len++; return len; } |
例3
1 2 3 4 5 6 | unsigned int strlen(const char*str){ assert(str); const char*p=str; while(*p++!=0); return p-str-1; } |
例4
1 2 3 4 5 | unsigned int strlen(const char*str){ assert(str); if(*str==0) return 0; else return(1+strlen(++str)); } |
例5
1 2 3 4 5 6 | size_t strlen(const char*s){ const char*sc; for(sc=s;*sc!='\0';++sc); return sc-s; } |
例6
1 2 3 4 5 | size_t strlen(const char * str) { const char *cp = str; while(*cp++) return(cp-str-1); } |
以上各種實現的方式都是大同小異的,有的用的是變數,有的用的是指針。
其中,最後一個用的是遞歸的方式。其實,在實現庫函數的時候,是規定不可以調用其他的庫函數的,這裡只是給大家一個方法,可以不用變數就可以實現strlen。