1.strlençåºå«sizeof
2.å
³äºc++ strncpy
strlençåºå«sizeof
strlen(char*ï¼å½æ°æ±çæ¯å符串çå®é é¿åº¦ï¼å®æ±å¾æ¹æ³æ¯ä»å¼å§å°éå°ç¬¬ä¸ä¸ª'\0'ï¼å¦æä½ åªå®ä¹æ²¡æç»å®èµåå¼ï¼è¿ä¸ªç»ææ¯ä¸å®çï¼å®ä¼ä»aaé¦å°åä¸ç´æ¾ä¸å»ï¼ç´å°éå°'\0'åæ¢ãchar aa[];cout<<strlen(aa)<<endl; //ç»ææ¯ä¸å®ç
char aa[]={ '\0'}; cout<<strlen(aa)<<endl; //ç»æ为0
char aa[]=jun; cout<<strlen(aa)<<endl; //ç»æ为3ï¼æ¤å¤ä¸å®å ¨æ£ç¡®ï¼ç»æå¯è½æ¯ä¸å®çï¼å¦æç¼è¯å¨å°å ¶ä»çéé¶å符串æ¾å¨aa[]çå åå°ååï¼strlenç»æè¿æ¯ä¸å®çï¼MSPå¨IARç¼è¯æµè¯è¿ï¼ï¼ä½æ¯å¨vc6ä¸ç»æ为3ï¼ç¼è¯å¨ä¼å¨âå¤èªå¨æ·»ä¸\0ãï¼
char aa[5]=hello;cout<<strlen(aa)<<endl; //ç»æ5ï¼æ¤å¤ä¸å®å ¨æ£ç¡®ï¼çç±å¦ä¸ï¼å 为å®é å åä¸ï¼helloåé¢ä¸ä¸å®æ¯ç©ºï¼å³\0ï¼ï¼ï¼è¿åºè§ç¼è¯å¨èå®ï¼
èsizeofï¼ï¼è¿åçæ¯åé声æåæå çå åæ°ï¼ä¸æ¯å®é é¿åº¦ï¼æ¤å¤sizeofä¸æ¯å½æ°ï¼ä» ä» æ¯ä¸ä¸ªååèè¿ç®ç¬¦ï¼strlenæ¯å½æ°ã
sizeof(aa) è¿å
int a[]; sizeof(a) è¿å ï¼æ ¹æ®è¯è¨intå c æ¯å个åè c++æ¯å个 java æ¯ä¸¤ä¸ªï¼
âsizeofæä½ç¬¦çç»æç±»åæ¯size_tï¼å®å¨å¤´æ件ä¸typedef为unsignedãintç±»åã
该类åä¿è¯è½å®¹çº³å®ç°æ建ç«çæ大对象çåè大å°ã
âsizeofæ¯ååèè¿ç®ç¬¦ï¼å ³é®åï¼ï¼strlenæ¯å½æ°ã
âsizeofå¯ä»¥ç¨ç±»åååæ°ï¼strlenåªè½ç¨char*ååæ°ï¼ä¸å¿ é¡»æ¯ä»¥''\0''ç»å°¾çã
sizeofè¿å¯ä»¥ç¨å½æ°ååæ°ï¼æ¯å¦ï¼
short f();
printf(%d\n,sizeof(f()));
è¾åºçç»ææ¯sizeof(shortï¼ï¼å³2ã
âæ°ç»åsizeofçåæ°ä¸éåï¼ä¼ éç»strlenå°±éå为æéäºã
â大é¨åç¼è¯ç¨åº å¨ç¼è¯çæ¶åå°±æsizeof计ç®è¿äºæ¯ç±»åææ¯åéçé¿åº¦ãè¿å°±æ¯sizeof(xï¼å¯ä»¥ç¨æ¥å®ä¹æ°ç»ç»´æ°çåå
char str[]=;
long a=strlen(str); //a=;
int b=sizeof(str); //èb=;
6.strlençç»æè¦å¨è¿è¡çæ¶åæè½è®¡ç®åºæ¥ï¼æ¯ç¨æ¥è®¡ç®å符串çé¿åº¦ï¼ä¸æ¯ç±»åå å åç大å°ã
7.sizeofåå¦ææ¯ç±»åå¿ é¡»å æ¬å¼§ï¼å¦ææ¯åéåå¯ä»¥ä¸å æ¬å¼§ãè¿æ¯å 为sizeofæ¯ä¸ªæä½ç¬¦ä¸æ¯ä¸ªå½æ°ã
âå½éç¨äºäºä¸ä¸ªç»æç±»åæ¶æåéï¼ sizeof è¿åå®é ç大å°ï¼
å½éç¨ä¸éæå°ç©ºé´æ°ç»ï¼ sizeof å½è¿å ¨é¨æ°ç»ç尺寸ã
sizeof æä½ç¬¦ä¸è½è¿åå¨æå°è¢«åæ´¾äºçæ°ç»æå¤é¨çæ°ç»ç尺寸
âæ°ç»ä½ä¸ºåæ°ä¼ ç»å½æ°æ¶ä¼ çæ¯æéèä¸æ¯æ°ç»ï¼ä¼ éçæ¯æ°ç»çé¦å°åï¼
å¦ï¼
fun(char [8])
fun(char [])
é½çä»·äº fun(char *)
å¨C++éåæ°ä¼ éæ°ç»æ°¸è¿é½æ¯ä¼ éæåæ°ç»é¦å ç´ çæéï¼ç¼è¯å¨ä¸ç¥éæ°ç»ç大å°
å¦ææ³å¨å½æ°å ç¥éæ°ç»ç大å°ï¼ éè¦è¿æ ·åï¼
è¿å ¥å½æ°åç¨memcpyæ·è´åºæ¥ï¼é¿åº¦ç±å¦ä¸ä¸ªå½¢åä¼ è¿å»
fun(unsiged char *p1,int len){ unsigned char* buf = new unsigned char[len+1] memcpy(buf,p1,len);}
æ们è½å¸¸å¨ç¨å° sizeof å strlen çæ¶åï¼é常æ¯è®¡ç®å符串æ°ç»çé¿åº¦
çäºä¸é¢ç详ç»è§£éï¼åç°ä¸¤è ç使ç¨è¿æ¯æåºå«çï¼ä»è¿ä¸ªä¾åå¯ä»¥çå¾å¾æ¸ æ¥ï¼
char str[]=;
int a=strlen(str); //a=; >>>> strlen 计ç®å符串çé¿åº¦ï¼ä»¥ç»æ符 0x 为å符串ç»æã
int b=sizeof(str); //èb=; >>>> sizeof 计ç®çåæ¯åé çæ°ç» str[] æå çå å空é´ç大å°ï¼ä¸åéé¢åå¨çå 容æ¹åã
ä¸é¢æ¯å¯¹éææ°ç»å¤ççç»æï¼å¦ææ¯å¯¹æéï¼ç»æå°±ä¸ä¸æ ·äº
char* ss = ;
sizeof(ss) ç»æ 4>>>>ssæ¯æåå符串常éçå符æéï¼sizeof è·å¾çæ¯ä¸ä¸ªæéçå¼æå ç空é´ï¼åºè¯¥æ¯é¿æ´åçï¼æ以æ¯4
sizeof(*ss) ç»æ 1>>>> *ssæ¯ç¬¬ä¸ä¸ªå符 å ¶å®å°±æ¯è·å¾äºå符串ç第ä¸ä½'0' æå çå å空é´ï¼æ¯charç±»åçï¼å äº 1 ä½
strlen(ss)= >>>> å¦æè¦è·å¾è¿ä¸ªå符串çé¿åº¦ï¼åä¸å®è¦ä½¿ç¨ strlen
sizeofè¿å对象æå ç¨çåè大å°. //æ£ç¡®
strlenè¿åå符个æ°. //æ£ç¡®
å¨ä½¿ç¨strlenæ¶ï¼æä¸ä¸ªå¾ç¹å«çæ åµï¼å°±æ¯æ°ç»åå°æéèåï¼
char Array[3] = { '0'};
sizeof(Array) == 3;
char *p = Array;
strlen(p) == 1;//sizeof(pï¼ç»æ为4
å¨ä¼ éä¸ä¸ªæ°ç»åå°ä¸ä¸ªå½æ°ä¸æ¶ï¼å®ä¼å®å ¨éå为ä¸ä¸ªæé
----------------------------------------------------------
çå®ä»¥ä¸ä½ æ¯å¦å¾æ¸ æ¥sizeofåstrlençåºå«äºå¢ï¼è¿ä¸æç½çè¯ï¼æ们çä¸é¢å 个ä¾åï¼ char* ss = ;
sizeof( ss) ç»æ 4 ===ãssæ¯æåå符串常éçå符æé
sizeof(*ss) ç»æ 1 ===ã*ssæ¯ç¬¬ä¸ä¸ªå符
大é¨åç¼è¯ç¨åº å¨ç¼è¯çæ¶åå°±æsizeof计ç®è¿äº æ¯ç±»åææ¯åéçé¿åº¦
è¿å°±æ¯sizeof(xï¼å¯ä»¥ç¨æ¥å®ä¹æ°ç»ç»´æ°çåå
char str[]=;
int a=strlen(str); //a=;
int b=sizeof(str); //èb=;
char ss[] = ;
sizeof(ss) ç»æ ===ãssæ¯æ°ç»ï¼è®¡ç®å°\0ä½ç½®ï¼å æ¤æ¯+1
sizeof(*ss) ç»æ 1 ===ã*ss æ¯ç¬¬ä¸ä¸ªå符
char ss[] = ;
sizeof(ss) ç»ææ¯ ===ãss表示å¨å åä¸çå¤§å° Ã1
strlen(ss) ç»ææ¯ ===ãstrlenæ¯ä¸ªå½æ°ï¼å é¨å®ç°æ¯ç¨ä¸ä¸ªå¾ªç¯è®¡ç®å°\0ä¹å为æ¢
int ss[] = ;
sizeof(ss) ç»æ ===ãss表示å¨å åä¸çå¤§å° Ã4
strlen(ss) é误 ===ãstrlençåæ°åªè½æ¯char* ä¸å¿ é¡»æ¯ä»¥'\0'ç»å°¾ç
char q[]=abc;
char p[]=a\n;
sizeof(q),sizeof(p),strlen(q),strlen(p);
ç»ææ¯ 4 3 3 2 class X{ int i; int j; char k;};X x;
cout<<sizeof(X)<<endl; ç»æ ===ãå åè¡¥é½
cout<<sizeof(x)<<endl; ç»æ åä¸ char szPath[MAX_PATH]
å¦æå¨å½æ°å è¿æ ·å®ä¹ï¼é£ä¹sizeof(szPathï¼å°ä¼æ¯MAX_PATHï¼ä½æ¯å°szPathä½ä¸ºèå声ææ¶ï¼void fun(char szPath[MAX_PATH]ï¼ï¼ï¼sizeof(szPathï¼å´ä¼æ¯4ï¼æé大å°ï¼
è¿æä¸ä½ç½åç说æä¹å¾å¥½ï¼
å ¶å®ç解 sizeof åªéè¦æä½ä¸ä¸ªè¦ç¹ï¼æ
ç¨åºåå¨åå¸æä¸ä¸ªåºåï¼æ ãéæåå¨æãè½å¤ä»ä»£ç ç´æ¥æä½ç对象ï¼å æ¬ä»»ä½ç±»åçåéãæéï¼é½æ¯å¨æ ä¸çï¼å¨æåéæåå¨åºæ¯é æ ä¸çæéæ¥é´æ¥æä½çãsizeof æä½ç¬¦ï¼è®¡ç®çæ¯å¯¹è±¡å¨æ ä¸çæå½±ä½ç§¯ï¼è®°ä½è¿ä¸ªå°±å¾å¤ä¸è¥¿é½å¾æ¸ æ¥äºã
char const * static_string = Hello;
sizeof(static_string) æ¯ sizeof ä¸ä¸ªæéï¼æä»¥å¨ bit system æ¯ 4
char stack_string[] = Hello;
sizeof(stack_string) æ¯ sizeof ä¸ä¸ªæ°ç»ï¼æä»¥æ¯ 6 * sizeof(char)
char * string = new char[6];
strncpy(string,Hello,6);
sizeof(string) æ¯ sizeof ä¸ä¸ªæéï¼æ以è¿æ¯ 4ãå第ä¸ä¸ªä¸åçæ¯ï¼è¿ä¸ªæéæåäºå¨æåå¨åºèä¸æ¯éæåå¨åºã
ä¸ç®¡æéæåçå 容å¨ä»ä¹å°æ¹ï¼sizeof å¾å°çé½æ¯æéçæ 大å°
C++ ä¸å¯¹å¼ç¨çå¤çæ¯è¾ç¹æ®ï¼sizeof ä¸ä¸ªå¼ç¨å¾å°çç»ææ¯ sizeof ä¸ä¸ªè¢«å¼ç¨ç对象ç大å°ï¼æ以
struct O{ int a,b,c,d,e,f,g,h;};int main(){ O & r = *new O; cout << sizeof(O) << endl; // cout << sizeof r << endl; // ä¹æ¯ system(PAUSE);}
r å¼ç¨çæ¯æ´ä¸ªç O 对象èä¸æ¯æå O çæéï¼æ以 sizeof r çç»æå sizeof O å®å ¨ç¸åã
èªå®ä¹å½æ°å®ç°strlenï¼ï¼å½æ°çåè½
ä¸é¢å ç§å®ç°strlenå½æ°çæºä»£ç 大家åè
ä¾1 #include<stdio.h>#include<assert.h>typedefunsignedintu_int;u_intMystrlen(constchar*str){ u_inti;assert(str!=NULL);for(i=0;str[i]!='\0';i++);returni;}ä¾2 intstrlen(constchar*str){ assert(str!=NULL);intlen=0;while((*str++)!='\0')len++;returnlen;}ä¾3 intstrlen(constchar*str){ assert(str);constchar*p=str;while(*p++!=NULL);returnp-str-1;}ä¾4 intstrlen(constchar*str){ assert(str);if(*str==NULL)return0;elsereturnï¼1+strlen(++str));}ä¾5 /***strlen-Findthelengthofastring*@s:Thestringtobesized*/size_tstrlen(constchar*s){ constchar*sc;for(sc=s;*sc!='\0';++sc)/*nothing*/;returnsc-s;}以ä¸åç§å®ç°çæ¹å¼é½æ¯å¤§åå°å¼çï¼æçç¨çæ¯åéï¼æçç¨çæ¯æéã
å ¶ä¸ï¼æåä¸ä¸ªç¨çæ¯éå½çæ¹å¼ãå ¶å®ï¼å¨å®ç°åºå½æ°çæ¶åï¼æ¯è§å®ä¸å¯ä»¥
è°ç¨å ¶ä»çåºå½æ°çï¼è¿éåªæ¯ç»å¤§å®¶ä¸ä¸ªæ¹æ³ï¼å¯ä»¥ä¸ç¨åéå°±å¯ä»¥å®ç°strlenã
å ³äºc++ strncpy
char* ch = "";
""åè¿æ个空å符
èstrncpy(ch5,ch,5);åªcopyäºchå5个å符ï¼å³ï¼æ²¡æ空å符
å 为strncpyæ¬èº«å°±æ¯ä¸ºäºæ¿æ¢æå符串ä¸çæä¸ä¸²å符ï¼åªæ¯åé¢çæ¿æ¢æäº
å½ç¨coutè¾åºæ¶ï¼éå°ç©ºå符ç»æ
å¨è¾åºch5æ¶æ°ç»åé¢çé½ä¸æ¯ç©ºå符ï¼æªåå§åï¼ï¼æ以è¾åºä¹±ç ·ç´å°éå°ç©ºå符为æ¢ï¼ä½ç½®æªå®ï¼Â·Â·