• 2022/11/24 [指针] 用函数调用实现字符串的复制(字符型指针)


    解题思路:定义一个函数copy_string 用来实现字符串复制的功能,在主函数中调用此函数,函数的形参和实参可以分别用字符数组名或字符指针变量

    (1)用字符数组名作为函数参数

    1. #include <stdio.h>
    2. int main()
    3. {
    4. void copy_string(char from[],char to[]);
    5. char a[] = "I am a teacher.";
    6. char b[] = "You are a student.";
    7. printf("string a=%s\nstring b=%s\n", a, b);
    8. printf("copy string a to string b:\n");
    9. copy_string(a, b);
    10. printf("\nstring a= %s\nstring b=%s\n", a, b);
    11. return 0;
    12. }
    13. void copy_string(char from[], char to[])
    14. {
    15. int i = 0;
    16. while (from[i] != '\0')
    17. {
    18. to[i] = from[i];
    19. i++;
    20. }
    21. to[i] = '\0';
    22. }

    (2)用字符型指针变量作实参
     

    1. #include<stdio.h>
    2. int main()
    3. {
    4. void copy_string(char from[], char to[]);
    5. char a[] = "I am a teacher.";
    6. char b[] = "You are a student.";
    7. char* from = a, * to = b;
    8. printf("string a= %s\nstring b=%s\n",a, b);
    9. printf("\ncopy string a to string b: \n");
    10. copy_string(from, to);//字符型指针变量作品作实参
    11. printf("string a=%s\nstring b=%s\n", a, b);
    12. return 0;
    13. }
    14. void copy_string(char from[], char to[])
    15. {
    16. int i = 0;
    17. while (from[i] != '\0')
    18. {
    19. to[i] = from[i];
    20. i++;
    21. }
    22. to[i] = '\0';
    23. }

    程序分析:指针变量from的值是a数组首元素的地址,指针变量to的值是b数组首元素的地址。它们作为实参,把a数组首元素的地址和b数组首元素的地址传递给形参数组名from和 to(它们实质上也是指针变量),其他与程序(1)相同。
     

    (3)用字符指针变量作形参和实参

    1. #include<stdio.h>
    2. int main()
    3. {
    4. void copy_string(char* from, char* to);
    5. char* a = "I am a teacher.";
    6. char b[] = "You are a student.";//定义b为字符数组
    7. char* p = b;
    8. printf("string a=%s\nstring b=%s\n", a, b);
    9. printf("\ncopy string a to string b: \n");
    10. copy_string(a, p);//通过p改变b
    11. printf("string a=%s\nstring b=%s\n", a, b);
    12. return 0;
    13. }
    14. void copy_string(char* from, char* to)
    15. {
    16. for (; *from != '\0'; from++, to++)
    17. {
    18. *to = *from;
    19. }
    20. *to = '\0';
    21. }

    程序改进:

    (1)将copy_string函数改写为

    void copy_string(char * from,char * to)

    {while (( * to= * from)!='\0')

    {to++;from++;}

    这是先赋值后判断,免去了最后还要给*to赋空字符额外的一步。

    (2) copy_string函数的函数体还可改为

    { while (( * to++= * from++)!='\0') ;}
    将++放在了一起,即赋值判断后再各自++

    或者写成:{while ( * from!='\0')
    *to++=* from++;

    * to='\0'; }

    (3)由于字符可以用其ASCII 码来代替(例如,“ch='a'”可用“ch= 97”代替,“while(ch!='a')”可以用“while(ch!=97)因此,“while( * from!='\0')”可以用‘while( * from! =0)”代替('\0'的ASCII代码
    而关系表达式“*from!=0”又可简化为*from”,这是因为* from的值不等于0,则表达式“* from”为真,同时“ * from!=0”也为真。因此“while( * from!=0)”和“while(* from)”是等价的。所以函数体可简化为

    { while ( * from)
    * to++= * from++;

    *to='\0’;
    }
    以上的while语句还等价于:while(*to++=*from++)

    等价于:while((*to++=*from++)!='\0')

    (4)也可以用字符数组名作函数形参,在函数中另定义两个指针变量pl,p2。函数copy_string可写为:

    void copy_string(char from[],char to[])

    {

    char * p1,* p2;
    pl=from; p2=to;
    while(( * p2++=* p1++)!='\0');

    }
     

  • 相关阅读:
    房间预定小程序怎么做_打造用户的专属空间预定小程序
    RocketMQ简介
    msf渗透练习-震网三代
    IP在网络通信中的重要作用
    第三天课程 RabbitMQ
    Bert基础(九)--Bert变体之ALBERT
    有人说考个PMP证两个星期搞定?
    uhttpd调试小结
    javascript常用方法总结及正则表达式
    36、Java——吃货联盟订餐系统(JDBC+MySQL+Apache DBUtils)
  • 原文地址:https://blog.csdn.net/fangzelin5/article/details/128027161