• 指针06 零基础入门学习C语言46


    第八章:指针06

     

    让编程改变世界

    Change the world by program


     

    字符指针作函数参数

    出错了:关于a[]和*a的一些区别! [codesyntax lang="c"]
    #include <stdio.h>
    
    void  main()
    {
          void copy_string( char *from, char *to );
    
          char *a = "I am a teacher.";
          char b[] = "You are a student."; //此处应该定义为char b[] = "You are a student.";
                                           //区别两者反汇编代码并解释其意义
                                           //谈谈五个区!
          printf("String a = %snString b = %sn", a, b);
          printf("copy string a to string b:n");
    
          copy_string(a, b);
    
          printf("nString a = %snString b = %sn", a, b);
    }
    
    void  copy_string( char *from, char *to )
    {
          for( ; *from != '�'; from++,to++)
          {
                *to = *from;
          }
    
          *to = '�';
    }
    [/codesyntax]  

    优化:对 copy_string 函数还可作简化

    方法一

    [codesyntax lang="c"]
    #include <stdio.h>
    
    void  main()
    {
          void copy_string( char *from, char *to );
    
          char *a = "I am a teacher.";
          char b[] = "You are a student.";
    
          printf("String a = %snString b = %sn", a, b);
          printf("copy string a to string b:n");
    
          copy_string(a, b);
    
          printf("nString a = %snString b = %sn", a, b);
    }
    
    void  copy_string( char *from, char *to )
    {
          while( (*to = *from) != '�' )
          {
                to++;
                from++;
          }
    }
    [/codesyntax]

    方法二

    [codesyntax lang="c"]
    #include <stdio.h>
    
    void  main()
    {
          void copy_string( char *from, char *to );
    
          char *a = "I am a teacher.";
          char b[] = "You are a student.";
    
          printf("String a = %snString b = %sn", a, b);
          printf("copy string a to string b:n");
    
          copy_string(a, b);
    
          printf("nString a = %snString b = %sn", a, b);
    }
    
    void  copy_string( char *from, char *to )
    {
          while( (*to++ = *from++) != '�')
          {
                ;
          }
    }
    [/codesyntax]

    方法三

    [codesyntax lang="c"]
    #include <stdio.h>
    
    void  main()
    {
          void copy_string( char *from, char *to );
    
          char *a = "I am a teacher.";
          char b[] = "You are a student.";
    
          printf("String a = %snString b = %sn", a, b);
          printf("copy string a to string b:n");
    
          copy_string(a, b);
    
          printf("nString a = %snString b = %sn", a, b);
    }
    
    void  copy_string( char *from, char *to )
    {
          while( *from != '�' )
          {
                *to++ = *from++;
          }
          *to = '�';
    }
    [/codesyntax]

    方法四

    [codesyntax lang="c"]
    #include <stdio.h>
    
    void  main()
    {
          void copy_string( char *from, char *to );
    
          char *a = "I am a teacher.";
          char b[] = "You are a student.";
    
          printf("String a = %snString b = %sn", a, b);
          printf("copy string a to string b:n");
    
          copy_string(a, b);
    
          printf("nString a = %snString b = %sn", a, b);
    }
    
    void  copy_string( char *from, char *to )
    {
          while( *to++ = *from++ )
          {
                ;    // '�' == 0
          }
    }
    [/codesyntax]

    方法五

    [codesyntax lang="c"]
    #include <stdio.h>
    
    void  main()
    {
          void copy_string( char *from, char *to );
    
          char *a = "I am a teacher.";
          char b[] = "You are a student.";
    
          printf("String a = %snString b = %sn", a, b);
          printf("copy string a to string b:n");
    
          copy_string(a, b);
    
          printf("nString a = %snString b = %sn", a, b);
    }
    
    void  copy_string( char *from, char *to )
    {
          for( ; *to++ = *from++; )
          {
                ;
          }
    }
    [/codesyntax]

    方法六

    [codesyntax lang="c"]
    #include <stdio.h>
    
    void  main()
    {
          void copy_string( char *from, char *to );
    
          char *a = "I am a teacher.";
          char b[] = "You are a student.";
    
          printf("String a = %snString b = %sn", a, b);
          printf("copy string a to string b:n");
    
          copy_string(a, b);
    
          printf("nString a = %snString b = %sn", a, b);
    }
    
    void  copy_string( char from[], char to[] )
    {
          char *p1, *p2;
    
          p1 = from;
          p2 = to;
    
          while( (*p2++ = *p1++) != '�' )
          {
                ;
          }
    }
    [/codesyntax] [buy] 获得所有教学视频、课件、源代码等资源打包 [/buy] [Downlink href='http://kuai.xunlei.com/d/LHAPZBNXYNES']视频下载[/Downlink]
  • 相关阅读:
    SpringCloud
    Springboot笔记
    SpringMVC学习随笔
    Spring中事务的理解
    Spring中Aop的理解
    FLutter 各种项目的目录创建
    flutter 打包Android 创建签名证书
    flutter web网站 第一次访问首屏页 空白卡顿 再显示
    flutter--报错--Flutter 调试时卡在 Installing buildappoutputsapkapp.apk
    flutter 顶部状态栏 底部栏 显示和隐藏
  • 原文地址:https://www.cnblogs.com/LoveFishC/p/3847022.html
Copyright © 2020-2023  润新知