• 指针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]
  • 相关阅读:
    第一个win8应用的制作过程
    win8开发-Xaml学习笔记一
    梦想成为“老板”的第二天
    梦想成为“老板”的第一天
    HTTP请求
    linux常用命令
    HTML中常用的标签
    HTML基本结构
    记录Django的settings文件常用配置
    Oracle数据泵expdp、impdp
  • 原文地址:https://www.cnblogs.com/LoveFishC/p/3847022.html
Copyright © 2020-2023  润新知