1.å符æéå¯ä»¥æåä¸ä¸ªå符串ã
æ们å¯ä»¥ç¨å符串常é对å符æéè¿è¡åå§åãä¾å¦ï¼æ说æè¯å¥ï¼
char *str = "This is a string.";
æ¯å¯¹å符æéè¿è¡åå§åãæ¤æ¶ï¼å符æéæåçæ¯ä¸ä¸ªå符串常éçé¦å°åï¼å³æåå符串çé¦å°åã
è¿éè¦æ³¨æå符æéä¸å符æ°ç»ä¹é´çåºå«ãä¾å¦ï¼æ说æè¯å¥ï¼
char string[ ]="This is a string.";
æ¤æ¶ï¼stringæ¯å符æ°ç»ï¼å®åæ¾äºä¸ä¸ªå符串ã
å符æésträ¸å符æ°ç»stringçåºå«æ¯ï¼stræ¯ä¸ä¸ªåéï¼å¯ä»¥æ¹åstr使å®æåä¸åçå符串ï¼ä½ä¸è½æ¹åstrææçå符串常éãstringæ¯ä¸ä¸ªæ°ç»ï¼å¯ä»¥æ¹åæ°ç»ä¸ä¿åçå
容ã
2.å®ä¾ï¼
char *str, *str1="This is another string.";
char string[100]="This is a string.";
åå¨ç¨åºä¸ï¼å¯ä»¥ä½¿ç¨å¦ä¸è¯å¥ï¼
str++; /* æéstrå 1 */
str = "This is a NEW string."; /* 使æéæåæ°çå符串常é */
str = str1; /* æ¹åæéstrçæå */
strcpy( string, "This is a NEW string.") /* æ¹åå符串ççå
容 */
strcat( string, str) /* è¿è¡ä¸²è¿æ¥æä½ */
å¨ç¨åºä¸ï¼ä¸è½è¿è¡å¦ä¸æä½ï¼
string++; /* ä¸è½å¯¹æ°ç»åè¿è¡++è¿ç® */
string = "This is a NEW string."; /* é误ç串æä½ */
string = str1; /* 对æ°ç»åä¸è½è¿è¡èµå¼ */
strcat(str, "This is a NEW string.") /* ä¸è½å¨strçåé¢è¿è¡ä¸²è¿æ¥ */
strcpy(str, string) /* ä¸è½åstrè¿è¡ä¸²å¤å¶ */
3ï¼å
¶å®è¯´æï¼
1) 以å符串形å¼åºç°çï¼ç¼è¯å¨é½ä¼ä¸ºè¯¥å符串èªå¨æ·»å ä¸ä¸ª0ä½ä¸ºç»æ符ï¼å¦å¨ä»£ç ä¸åï¼"abc",é£ä¹ç¼è¯å¨å¸®ä½ åå¨çæ¯"abc\0"
2) "abc"æ¯å¸¸éåï¼çæ¡æ¯ææ¶æ¯ï¼ææ¶ä¸æ¯ã
ä¸æ¯å¸¸éçæ
åµï¼"abc"ä½ä¸ºå符æ°ç»åå§å¼çæ¶åå°±ä¸æ¯ï¼å¦
char str[] = "abc";
å 为å®ä¹çæ¯ä¸ä¸ªå符æ°ç»ï¼æ以就ç¸å½äºå®ä¹äºä¸äºç©ºé´æ¥åæ¾"abc"ï¼èåå 为å符æ°ç»å°±æ¯æå符ä¸ä¸ªä¸ä¸ªå°åæ¾çï¼æ以ç¼è¯å¨æè¿ä¸ªè¯å¥è§£æ为 char str[3] = {'a','b','c'};åæ ¹æ®ä¸é¢çæ»ç»1ï¼æ以char str[] = "abc";çæç»ç»ææ¯ char str[4] = {'a','b','c','\0'};
åä¸ä¸æ©å±ï¼å¦æchar str[] = "abc";æ¯å¨å½æ°å
é¨åçè¯ï¼é£ä¹è¿éç"abc\0"å 为ä¸æ¯å¸¸éï¼æ以åºè¯¥è¢«æ¾å¨æ ä¸ã
æ¯å¸¸éçæ
åµï¼ æ"abc"èµç»ä¸ä¸ªå符æéåéæ¶ï¼å¦
char* ptr = "abc";
å 为å®ä¹çæ¯ä¸ä¸ªæ®éæéï¼å¹¶æ²¡æå®ä¹ç©ºé´æ¥åæ¾"abc"ï¼æ以ç¼è¯å¨å¾å¸®æ们æ¾å°æ¹æ¥æ¾"abc"ï¼æ¾ç¶ï¼æè¿éç"abc"å½æ常é并æå®æ¾å°ç¨åºç常éåºæ¯ç¼è¯å¨æåéçéæ©ãæ以尽管ptrçç±»åä¸æ¯const char*ï¼å¹¶ä¸ptr[0] = 'x';ä¹è½ç¼è¯éè¿ï¼ä½æ¯æ§è¡ptr[0] = 'x';å°±ä¼åçè¿è¡æ¶å¼å¸¸ï¼å 为è¿ä¸ªè¯å¥è¯å¾å»ä¿®æ¹ç¨åºå¸¸éåºä¸çä¸è¥¿ã
è®°å¾åªæ¬ä¹¦ä¸æ¾ç»è¯´è¿char* ptr = "abc";è¿ç§åæ³åæ¥å¨c++æ åä¸æ¯ä¸å
许çï¼ä½æ¯å 为è¿ç§åæ³å¨cä¸å®å¨æ¯å¤ªå¤äºï¼ä¸ºäºå
¼å®¹cï¼ä¸å
许ä¹å¾å
许ãè½ç¶å
许ï¼
ä½æ¯å»ºè®®çåæ³åºè¯¥æ¯const char* ptr = "abc";è¿æ ·å¦æåé¢åptr[0] = 'x'çè¯ç¼è¯å¨å°±ä¸ä¼è®©å®ç¼è¯éè¿ï¼ä¹å°±é¿å
äºä¸é¢è¯´çè¿è¡æ¶å¼å¸¸ã
åæ©å±ä¸ä¸ï¼å¦æchar* ptr = "abc";åå¨å½æ°ä½å
ï¼é£ä¹è½ç¶è¿éç"abc\0"被
æ¾å¨å¸¸éåºä¸ï¼ä½æ¯ptræ¬èº«åªæ¯ä¸ä¸ªæ®éçæéåéï¼æ以ptræ¯è¢«æ¾å¨æ ä¸ç, åªä¸è¿æ¯å®ææåçä¸è¥¿è¢«æ¾å¨å¸¸éåºç½¢äºã
3) æ°ç»çç±»åæ¯ç±è¯¥æ°ç»æåæ¾çä¸è¥¿çç±»å以åæ°ç»æ¬èº«ç大å°å³å®çãå¦char s1[3]åchar s2[4]ï¼s1çç±»åå°±æ¯char[3]ï¼s2çç±»åå°±æ¯char[4]ï¼
ä¹å°±æ¯è¯´å°½ç®¡s1ås2é½æ¯å符æ°ç»ï¼ä½ä¸¤è
çç±»åå´æ¯ä¸åçã
4) å符串常éçç±»åå¯ä»¥ç解为ç¸åºå符常éæ°ç»çç±»åï¼
å¦"abcdef"çç±»åå°±å¯ä»¥çææ¯const char[7]
5) sizeofæ¯ç¨æ¥æ±ç±»åçåèæ°çãå¦int a;é£ä¹æ 论sizeof(int)æè
æ¯sizeof(a)é½æ¯çäº4ï¼å 为sizeof(a)å
¶å®å°±æ¯sizeof(type of a)
6) 对äºå½æ°åæ°å表ä¸ç以æ°ç»ç±»å书åçå½¢å¼åæ°ï¼ç¼è¯å¨æå
¶è§£é为æ®éçæéç±»åï¼å¦å¯¹äºvoid func(char sa[100],int ia[20],char *p)
åsaçç±»å为char*ï¼iaçç±»å为int*ï¼pçç±»å为char*
7) æ ¹æ®ä¸é¢çæ»ç»ï¼æ¥å®æä¸ä¸ï¼
对äºchar str[] = "abcdef";å°±æsizeof(str) == 7,å 为strçç±»åæ¯char[7]ï¼
ä¹æsizeof("abcdef") == 7ï¼å 为"abcdef"çç±»åæ¯const char[7]ã
对äºchar *ptr = "abcdef";å°±æsizeof(ptr) == 4ï¼å 为ptrçç±»åæ¯char*ã
对äºchar str2[10] = "abcdef";å°±æsizeof(str2) == 10ï¼å 为str2çç±»åæ¯char[10]ã
对äºvoid func(char sa[100],int ia[20],char *p);
å°±æsizeof(sa) == sizeof(ia) == sizeof(p) == 4ï¼
å 为saçç±»åæ¯char*ï¼iaçç±»åæ¯int*ï¼pçç±»åæ¯char*ã
4.åºå«ï¼
(1)å符æ°ç»ç±è¥å¹²ä¸ªå
ç´ ç»æï¼æ¯ä¸ªå
ç´ ä¸åæ¾å符串çä¸ä¸ªå符ï¼èå符æéåéä¸åæ¾çæ¯å符串çé¦å°åã
(2)åå§åæ¹å¼ä¸åã对å符æ°ç»åå§åè¦ç¨staticåå¨ç±»å«ï¼å¨ç¼è¯æ¶è¿è¡ãè对å符æéåéåå§åä¸å¿
å staticï¼å¨å®é
æ§è¡æ¶è¿è¡ã
(3)èµå¼æ¹å¼ä¸åã对å符æ°ç»ä¸è½æ´ä½èµå¼ï¼åªè½è½¬åæ份éï¼å¯¹å个å
ç´ è¿è¡ãèå符æéåéèµå¼å¯æ´ä½è¿è¡ã
ä¾å¦ï¼
char s[10]ï¼
s= \"C++\"ï¼/*éï¼sæ¯å¸¸éï¼æè½è¢«èµå¼*/
(4)å¨å®ä¹ä¸ä¸ªå符æ°ç»æ¶ï¼ç¼è¯æ¶å³å·²åé
å
ååå
ï¼æç¡®å®çå°åãèå®ä¹ä¸ä¸ªå符æéåéæ¶ï¼ç»æéåéåé
å
ååå
ï¼ä½è¯¥æéåéå
·ä½æååªä¸ªå符串ï¼å¹¶ä¸ç¥éï¼å³æéåéåæ¾çå°åä¸ç¡®å®ãä¾å¦ï¼
char a[10];
char *p;
scanf(\"%s\"ï¼s);/*æ£ç¡®*/
scanf(\"%s\"ï¼p)ï¼/*é常å±é©ï¼pçå¼å¨æ*/
(5)å符æéåéçå¼å¯ä»¥æ¹åï¼å符æ°ç»åæ¯ä¸ä¸ªå¸¸éï¼ä¸è½æ¹åãä¾å¦ï¼æç®åç¨åºï¼
#include <stdio.h>
int main()
{
char *s = "china man";
s+= 6;
printf("%s\n",s);
getchar();
return 0;
}
è¿è¡ç»æï¼man
温馨提示:答案为网友推荐,仅供参考