copy()函数技术推演

/***
str_copy.c
***/
#include<stdio.h>

void copy_str21(char *from, char *to)
{
    for(; *from != '\0'; from++,to++)
    {
        *to = *from;
    }
    *to = '\0';

    return;
}

int main()
{
    char *from = "abcd";
    char buf2[100];
    copy_str21(from,buf2);
    printf("buf2:%s\n",buf2);
    return 0;
}

程序内存四区分析:

 

char *from = "abcd";  操作系统在在常量区分配一个内存存放”abcd”,在栈区定义分配一块内存,取名位from,from指向的这块内存存储“abcd”的首地址。

char buf2[100]; 操作系统在栈区分配一块内存,开头与栈区增长方向相反,大小为100个字节

copy_str21(from,buf2);        程序进入到该函数中

void copy_str21(char *from, char *to)        操作系统在继续在栈中分配两块内存,分别用to和from指向这两块内存。并且把mian函数中的from,to地址首部存放在该函数中分配到from,to指针指向的内存

 

for(; *from != '\0'; from++,to++)

{

       *to = *from;

}

copy_str21函数中from指针存储的地址是常量“abcd”的首地址,to指针存储的是mian函数中buf2数组的首地址。然后把from指针不断后移区内容赋值给to指针不断后移所指向的内存空间。

*to = '\0';      由于buf2没有进行初始化,所以最后要在结束的时候把最后面的内存赋值0,作为C语言字符串的结尾。

然后程序返回,copy_str21栈地址回收,主函数打印buf2字符数组。

 

copy函数技术演练

/***
str_copy.c
***/
#include<stdio.h>

void copy_str21(char *from, char *to)
{
    for(; *from != '\0'; from++,to++)
    {
        *to = *from;
    }
    *to = '\0';

    return;
}

void copy_str22(char *from,char *to)
{
    for(;*from != '\0';)
    {
        *to++ = *from++;
    }   
    *to = '\0';
    return ;
}

void copy_str23(char *from,char *to)
{
    while( (*to = *from) != '\0')
    {
        from++;
        to++;
    }
}

void copy_str24(char *from,char *to)
{
    while((*to++ = *from++) != '\0')
    {
        ;
    }
}

void copy_str25(char *from,char *to)
{
    while( (*to++ = *from++) );
}
int main()
{
    char *from = "abcd";
    char buf1[100];
    char buf2[100];
    char buf3[100];
    char buf4[100];
    char buf5[100];

    copy_str21(from,buf1);
    printf("buf1:%s\n",buf1);
    copy_str22(from,buf2);
    printf("buf2:%s\n",buf2);
    copy_str23(from,buf3);
    printf("buf3:%s\n",buf3);
    copy_str24(from,buf4);
    printf("buf4:%s\n",buf4);
    copy_str25(from,buf5);
    printf("buf5:%s\n",buf5);
    return 0;
}

运行结果:

exbot@ubuntu:~/shareWin/CAndC++/20190924$ gcc strcopy.c -o strcopy -g

exbot@ubuntu:~/shareWin/CAndC++/20190924$ ./strcopy

buf1:abcd

buf2:abcd

buf3:abcd

buf4:abcd

buf5:abcd

/***
copy_str.c
***/
#include<stdio.h>

int copy_str26_good(char *from,char *to)
{
    if(from == NULL || to == NULL)
    {
        return -1;
    }

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

    return ;
}


int copy_str27_verygood(char *from,char *to)
{
    if(from == NULL || to == NULL)
    {
        return -1;
    }

    char *tempfrom = from;
    char *tempto = to;
    while(*tempto++ = *tempfrom++);

    return ;
}
int main()
{
    int ret = 0;
    char *from = "abcd";
    char buf[100];
    char buf1[100];

    //传进来的指针如果被初始化指向null的话,是不需要被修改的
    //因为null和0是操作系统内部一块无法访问和修改的地址空间存储的
    //所以提前对传进来的指针进行判断,如果为null,则返回错误
    ret = copy_str26_good(from,buf);
    if(-1 == ret)
    {
        printf("ponit p or buf is null\n");
    }
    else
    {
        printf("copy success buf = %s\n",buf);
}

//不要轻易改变形参的值,要引入一个辅助的变量指针,把形参接过来
    ret = copy_str27_verygood(from,buf1);
    if(-1 == ret)
    {
        printf("ponit p or buf is null\n");
    }
    else
    {
        printf("copy success buf1 = %s\n",buf1);
    }
    return 0;
}

运行结果:

exbot@ubuntu:~/shareWin/CAndC++/20190924$ ./copy_str

copy success buf = abcd

copy success buf1 = abcd

posted @ 2019-09-24 21:39  王清河  阅读(358)  评论(0编辑  收藏  举报