zoukankan      html  css  js  c++  java
  • 指针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]
  • 相关阅读:
    浅谈HTTP协议(下)
    HTML5 十大新特性(四)——Canvas绘图
    FPGA 机器学习之BP神经网络1
    FPGA模式识别之亮点,光点检测
    FPGA模式识别之数字检测
    FPGA大数据之我认为的明天
    FPGA 机器学习之BP神经网络2
    FPGA片外存储器之DDR2控制器2
    FPGA图像处理之边缘检测,中值滤波,图像均衡1。
    FPGA,verilog程序技巧之状态机与稳定性,高速
  • 原文地址:https://www.cnblogs.com/LoveFishC/p/3846043.html
Copyright © 2011-2022 走看看