• 关于strcpy的一道程序


    #include<stdio.h>
    #include<string.h>
    void main()
    {
     char s[]="123456789";
     char d[]="123";
     strcpy(d,s);
     printf("d=%s,\ns=%s",d,s);
    }

    执行结果:d=56789,

              s=123456789

    分析:首先要解释一下,char s[]="123456789"; char d[]="123"; 这样定义的数组和变量存放在栈内存中。 
        栈内存是一个自顶向下分布的数据结构,那么越先定义的变量地址就越高,越后定义的地址就越低。 
        s比d定义在前,那么s得到了高地址,而d得到了相对低的地址,那么内存中的存放形式就是 
                          d[]    <- | ->          s[] 
                  '1' '2' '3' '\0'| '1' '2' '3' '4' '5' '6' '7' '8' '9' '\0’

     字符串拷贝后: 
                  '1' '2' '3' '4 '| '5' '6' '7' '8' '9' '\0' '7' '8' '9' '\0'  中间的‘|’表示s[]的起始位置。 
      所以此时输出的是s的值是 '5' '6' '7' '8' '9' '\0',

    #######################新内容#######################
    已知strcpy函数的原型是:
    char * strcpy(char * strDest,const char * strSrc);

    实现代码
    char * strcpy(char * strDest,const char * strSrc)
    {
    if ((strDest==NULL)||(strSrc==NULL))

    throw "Invalid argument(s)";

    char * strDestCopy=strDest;

    while ((*strDest++=*strSrc++)!='\0');

    return strDestCopy;
    }

  • 相关阅读:
    JSP ——第九次作业
    JSP ——第八次作业
    JSP ——第七次作业-mail_system
    JSP ——第六次作业
    JSP——第五次作业
    软件测试——第二次
    JSP作业 四——2
    JSP 作业 四 —(1)
    JSP 第三次作业
    NSData的同步下载与NSConnection的同步下载
  • 原文地址:https://www.cnblogs.com/paulbai/p/2710202.html
Copyright © 2020-2023  润新知