將一個字符串從一個函數(shù)傳遞到另一個函數(shù),可以使用傳地址的方式,即用字符數(shù)組名或字符指針變量作參數(shù)。有以下四種情況:
| 實參 | 形參 | 1 | 數(shù)組名 | 數(shù)組名 | 2 | 數(shù)組名 | 字符指針變量 | 3 | 字符指針變量 | 字符指針變量 | 4 | 字符指針變量 | 數(shù)組名 |
[例] 用函數(shù)調(diào)用實現(xiàn)字符串的復(fù)制。
(1)用字符數(shù)組作參數(shù)。
void | copy_string(char from[], char to[]) | { | int i=0; | | while(from != '\0') | | { to = from; i++; } | | to = '\0'; | } | | | | main | () | { | char a[] = "I am a teacher."; | | char b[] = "you are a student."; | | printf("string_a =%s\n string_b =%s\n", a,b); | | copy_string(a,b); | | printf("string_a =%s\n string_b =%s\n", a,b); | } | |
main()函數(shù)可以改寫為(使用字符指針):
main | () | { | char *a = "I am a teacher."; | | char *b = "you are a student."; | | printf("string_a =%s\n string_b =%s\n", a,b); | | copy_string(a,b); | | printf("string_a =%s\n string_b =%s\n", a,b); | } | |
(2)形參用字符指針。
void | (char *from, char *to) | { | | | for(; *form != '\0'; from++, to++) | | *to = *from; | | *to = '\0'; | } | | | |
main | () | { | char *a = "I am a teacher."; | | char *b = "you are a student."; | | printf("string_a =%s\n string_b =%s\n", a,b); | | copy_string(a,b); | | printf("string_a =%s\n string_b =%s\n", a,b); | } | |
(3)對copy_string函數(shù)的幾種簡化
void copy_string(char *from, char *to) | { | while((*to=*from) !='\0') | {to++; from++} | } |
| ①
*to=*from是一個賦值表達式,其值等于*from。to++和from++分別使指針指向下一個字節(jié)。
先執(zhí)行賦值表達式,再判賦值表達式的值(等于*from)是否為'\0',因此,from串中的結(jié)尾字符'\0'被賦值給to。
| void | copy_string(char *from, char *to) | { | | | while((*to++=*from++) != '\0'); | } | |
| ②
*to++=*from++先執(zhí)行*to=*from,再使to、from分別加1。
| void | copy_string(char *from, char *to) | { | | | while(*from != '\0') | | *to++ = *from++; | | *to = '\0'; | } | |
| ③ 當遇到*from='\0'時,不執(zhí)行賦值運算*to++=*from++,因此,最后應(yīng)加一句*to='\0'。
| void | copy_string(char *from, char *to) | { | | | while(*to++=*from++); | } | |
| ④
與第②種簡化相同,當*from='\0'時,表達式*to++=*from++的值等于'\0'(假),結(jié)束while循環(huán)。
| void | copy_string(char *from, char *to) | { | | | for(;(*to++ =*from++) !=0;); | 或 | for(;*to++ =*from++;); | } | |
| ⑤ for循環(huán)的結(jié)束條件是表達式*to++ =*from++的值(即*from的值)等于'\0'。且form中的'\0'已被賦給to。注意'\0'的ASCII碼是不是0。
| void | copy_string(char from[], char to[]) | { | char *p1, *p2; | | p1 = from; p2 = to; | | while((*p2++=p1++) !='\0') | } | |
| ⑥
形參用數(shù)組,使用局部指針變量指向形參數(shù)組。
|
(4).字符串指針作函數(shù)的參數(shù)
字符串指針作函數(shù)的參數(shù),與前面介紹的數(shù)組指針作函數(shù)參數(shù)沒有本質(zhì)的區(qū)別,函數(shù)間傳遞的都是地址值,所不同的僅是指針指向?qū)ο蟮念愋筒煌选? 例5 用字符串指針作函數(shù)參數(shù),將輸入的一個字符串復(fù)制到另一個字符串中(重點例題)。 #include "stdio.h" main() { void copy_s(char *,char *); char a[20],b[30]; gets(a); copy_s(a,b); puts(b); } void copy_s(char *str1,char *str2) { while((*str2=*str1)!='\0') { str1++; str2++; } } 程序在主函數(shù)中定義了兩個字符數(shù)組a和b,a數(shù)組由gets()函數(shù)獲得,b數(shù)組通過調(diào)用copy_s()函數(shù)自動生成。copy_s()函數(shù)有兩個指向字符串的形參,其功能是將str1指向的字符串復(fù)制到str2中。 copy_s()函數(shù)體由一個while語句構(gòu)成,其條件表達式“(*str2=* str 1)!='\0'”的計算過程是:先進行賦值*str2=*str1,將指針str1的內(nèi)容送到指針str2指向的存儲位置,然后再判斷所賦值的字符是否是串結(jié)束標記'\0'。若條件表達式成立,即當前復(fù)制的字符不是串結(jié)束標記,則繼續(xù)循環(huán),復(fù)制下一個字符;否則,退出循環(huán),完成串復(fù)制。 在主函數(shù)中調(diào)用copy_s()函數(shù)時,必須注意兩個實參地址的順序,第一個實參是原始字符串首地址,第二個實參是目標字符串首地址。本程序調(diào)用時使用的是存儲兩個字符串的字符數(shù)組名。當然,在函數(shù)調(diào)用時,也可以直接使用字符串指針變量作函數(shù)實參,如下是修改后的main()函數(shù): main() { void copy_s(char *,char *); char a[20],b[30],*pa=a,*pb=b; gets(pa); copy_s(pa,pb); puts(pb); } 事實上,這種使用字符串的方法在C語言程序中更為常用。
|