strcpy - 我的实现 [英] strcpy - my implementation

查看:102
本文介绍了strcpy - 我的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的strcpy库函数实现。我想

喜欢改进评论:

/ *我的版本strcpy - 一个C库函数* /


#include< stdio.h>

#include< stdlib.h>

#include< unistd.h>

#include< string.h>


enum {ARRSIZE = 101};


char * my_strcpy(char *,char *);


int main(int argc,char ** argv)

{

char * pc;


char arr_in [ARRSIZE];

char arr_out [ARRSIZE];


memset(arr_in,''\''', ARRSIZE);

memset(arr_out,''\'',ARRSIZE);

if(2!= argc)

{

perror(USAGE:./ exec \你的输入\" \ n");

退出(EXIT_FAILURE);

}

其他

{

strcpy(arr_in,argv [1]);

}


pc = my_strcpy(arr_out,arr_in);


while(* pc )

{

printf(" * pc =%c \ nn",* pc ++);

}


返回EXIT_SUCCESS;

}


char * my_strcpy(char * arr_out,char * arr_in)

{

char * pc;


pc = arr_out;


while((* arr_out ++ = * arr_in ++ ));


返回电脑;

}

===============输出======================


[arnuld @ dune ztest] $ gcc -ansi -pedantic -Wall - Wextra check_STRCPY.c

[arnuld @ dune ztest] $ ./a.out赞

* pc = l

* pc = i

* pc = k

* pc = e

[arnuld @ dune ztest] $


它工作正常没有麻烦。现在,如果您更改

my_strcpy中的最后一次回复来自return pc返回return arr_out,然后在
main()中循环时根本不打印任何东西。我真的不理解它。

使用thr数组名称会给出一个指向它的第一个元素的指针但是int htis

它给出一个指向它最后一个元素的指针。为什么?这就是我为什么
引入额外的char * pc的原因。首先。


-
www.lispmachine.wordpress.com

我的电子邮件是@上面的博客。

谷歌群组被阻止。原因:过度垃圾邮件

I have created my own implementation of strcpy library function. I would
like to have comments for improvements:
/* My version of "strcpy - a C Library Function */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

enum { ARRSIZE = 101 };

char* my_strcpy( char*, char* );

int main( int argc, char** argv )
{
char* pc;

char arr_in[ARRSIZE];
char arr_out[ARRSIZE];

memset( arr_in, ''\0'', ARRSIZE );
memset( arr_out, ''\0'', ARRSIZE );
if( 2 != argc )
{
perror("USAGE: ./exec \" your input \"\n");
exit( EXIT_FAILURE );
}
else
{
strcpy( arr_in , argv[1] );
}

pc = my_strcpy( arr_out, arr_in );

while( *pc )
{
printf("*pc = %c\n", *pc++);
}

return EXIT_SUCCESS;
}

char* my_strcpy( char* arr_out, char* arr_in )
{
char* pc;

pc = arr_out;

while( (*arr_out++ = *arr_in++) ) ;

return pc;
}
=============== OUTPUT ======================

[arnuld@dune ztest]$ gcc -ansi -pedantic -Wall -Wextra check_STRCPY.c
[arnuld@dune ztest]$ ./a.out like
*pc = l
*pc = i
*pc = k
*pc = e
[arnuld@dune ztest]$

It works fine without troubles. Now if you change the last return call in
my_strcpy from "return pc" to return "return arr_out", then while loop in
main() will not print anything at all. I really did not understand it.
Using thr array name will give a pointer to its 1st element but int htis
case it is giving a pointer to its last element. Why ? Thats why I
introduced the extra "char* pc" in first place.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

推荐答案

gcc -ansi -pedantic -Wall -Wextra check_STRCPY.c

[ arnuld @ dune ztest]
gcc -ansi -pedantic -Wall -Wextra check_STRCPY.c
[arnuld@dune ztest]


./ a.out like

* pc = l

* pc = i

* pc = k

* pc = e

[arnuld @ dune ztest]
./a.out like
*pc = l
*pc = i
*pc = k
*pc = e
[arnuld@dune ztest]





它运行正常没有麻烦。现在,如果您更改

my_strcpy中的最后一次回复来自return pc返回return arr_out,然后在
main()中循环时根本不打印任何东西。我真的不理解它。

使用thr数组名称会给出一个指向它的第一个元素的指针但是int htis

它给出一个指向它最后一个元素的指针。为什么?这就是我为什么
引入额外的char * pc的原因。首先。


-
www.lispmachine.wordpress.com

我的电子邮件是@上面的博客。

谷歌群组被阻止。原因:过度垃圾邮件



It works fine without troubles. Now if you change the last return call in
my_strcpy from "return pc" to return "return arr_out", then while loop in
main() will not print anything at all. I really did not understand it.
Using thr array name will give a pointer to its 1st element but int htis
case it is giving a pointer to its last element. Why ? Thats why I
introduced the extra "char* pc" in first place.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming


这篇关于strcpy - 我的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆