字符串的引用方式
(1)用字符数组存放一个字符串 ——>char str[];(还可以操作单个字符)
(2)用字符指针变量指向一个字符串常量 ——>char * str;
示例代码1
//(1)字符数组名做函数参数(复制 a数组到 b数组)
#include <stdio.h>
int main(){
void copy_string (char from[],char to[]);
char a[] = "i am a teacher";
char b[] = "you are a programmer";
printf("string a = %s\n string b = %s\n",a,b);
printf("copy string a too string b:\n");
copy_string(a,b);
printf("\nstring a = %s\n string b = %s\n",a,b);
return 0;
}
void copy_string(char from[],char to[]){
int i = 0;
while(from[i] != '\0'){
to[i] = from[i];
i++;
}
to[i] = '\0';
}
示例代码2
//(2)字符指针变量做函数参数(复制 a数组到 b数组)
#include<stdio.h>
int main(){
void copy_string(char *from,char *to);
char *a = "I am a teacher";
char b[] = "You are a programmer";
char *p = b;
printf("string a=%s\nstring b=%s\n",a,b);
printf("copy string a to string b:\n");
copy_string(a,p);
printf("\n string a=%s\n string b=%s\n",a,b);
return 0;
}
void copy_string(char *from,char *to){
for(;*from!='\0';from++,to++)
*to = *from;
*to = '\0';
}