將一個字符串從一個函數傳遞到另一個函數,可以使用傳地址的方式,即用字符數組名或字符指針變量作參數。有以下四種情況:
| 實參 | 形參 | 1 | 數組名 | 數組名 | 2 | 數組名 | 字符指針變量 | 3 | 字符指針變量 | 字符指針變量 | 4 | 字符指針變量 | 數組名 |
[例] 用函數調用實現字符串的復制。
(1)用字符數組作參數。
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()函數可以改寫為(使用字符指針):
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函數的幾種簡化
void copy_string(char *from, char *to) | { | while((*to=*from) !='\0') | {to++; from++} | } |
| ①
*to=*from是一個賦值表達式,其值等于*from。to++和from++分別使指針指向下一個字節。
先執行賦值表達式,再判賦值表達式的值(等于*from)是否為'\0',因此,from串中的結尾字符'\0'被賦值給to。
| void | copy_string(char *from, char *to) | { | | | while((*to++=*from++) != '\0'); | } | |
| ②
*to++=*from++先執行*to=*from,再使to、from分別加1。
| void | copy_string(char *from, char *to) | { | | | while(*from != '\0') | | *to++ = *from++; | | *to = '\0'; | } | |
| ③ 當遇到*from='\0'時,不執行賦值運算*to++=*from++,因此,最后應加一句*to='\0'。
| void | copy_string(char *from, char *to) | { | | | while(*to++=*from++); | } | |
| ④
與第②種簡化相同,當*from='\0'時,表達式*to++=*from++的值等于'\0'(假),結束while循環。
| void | copy_string(char *from, char *to) | { | | | for(;(*to++ =*from++) !=0;); | 或 | for(;*to++ =*from++;); | } | |
| ⑤ for循環的結束條件是表達式*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') | } | |
| ⑥
形參用數組,使用局部指針變量指向形參數組。
|
(4).字符串指針作函數的參數
字符串指針作函數的參數,與前面介紹的數組指針作函數參數沒有本質的區別,函數間傳遞的都是地址值,所不同的僅是指針指向對象的類型不同而已。 例5 用字符串指針作函數參數,將輸入的一個字符串復制到另一個字符串中(重點例題)。 #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++; } } 程序在主函數中定義了兩個字符數組a和b,a數組由gets()函數獲得,b數組通過調用copy_s()函數自動生成。copy_s()函數有兩個指向字符串的形參,其功能是將str1指向的字符串復制到str2中。 copy_s()函數體由一個while語句構成,其條件表達式“(*str2=* str 1)!='\0'”的計算過程是:先進行賦值*str2=*str1,將指針str1的內容送到指針str2指向的存儲位置,然后再判斷所賦值的字符是否是串結束標記'\0'。若條件表達式成立,即當前復制的字符不是串結束標記,則繼續循環,復制下一個字符;否則,退出循環,完成串復制。 在主函數中調用copy_s()函數時,必須注意兩個實參地址的順序,第一個實參是原始字符串首地址,第二個實參是目標字符串首地址。本程序調用時使用的是存儲兩個字符串的字符數組名。當然,在函數調用時,也可以直接使用字符串指針變量作函數實參,如下是修改后的main()函數: main() { void copy_s(char *,char *); char a[20],b[30],*pa=a,*pb=b; gets(pa); copy_s(pa,pb); puts(pb); } 事實上,這種使用字符串的方法在C語言程序中更為常用。
|