1.入门等级写法
int main()
{
char buf1[20] = "you kn0w I love you";
char buf2[20];
int i;
for (i=0;*(buf1+i)!='\0';i++)
{
*(buf2 + i) = *(buf1 + i);
}
*(buf2 + i) = '\0';
printf("buf1=%s\n", buf1);
printf("buf2=%s\n", buf2);
return 0;
}
2.字符串做函数参数进行函数调用
void copy_Str(char *from,char *to)
{
for (;*from!='\0';from++,to++)
{
*to = *from;
}
*to = '\0';
return;
}
int main()
{
char buf1[20] = "you know I love you";
char buf2[20];
copy_Str(buf1, buf2);
printf("buf1=%s\n", buf1);
printf("buf2=%s\n", buf2);
return 0;
}
2.1 字符串拷贝函数的改进1
void copy_Str1(char* from, char* to)
{
while ((*to=*from)!='\0')
{
from++;
to++;
}
return;
}
2.2 字符串拷贝函数的改进2
void copy_Str2(char* from, char* to)
{
while ((*to++ = *from++) != '\0')
{
;
}
return;
}
2.3字符串拷贝函数的改进3
void copy_Str3(char* from, char* to)
{
while ((*to++ = *from++)){}
return;
}
2.4 字符串拷贝函数的改进4
void copy_Str4(char* from, char* to)
{
char* tempFrom = from;
char* tempTo = to;
while ((*tempTo++ = *tempFrom++));
return;
}
注:一般不要随便改变形参指针的指向 ,定义两个临时的指针变量接收形参的地址