我的strcpy有什么问题?

编程入门 行业动态 更新时间:2024-10-25 02:19:15
本文介绍了我的strcpy有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让 strcpy 自己。它应该工作,我甚至复制和粘贴(几乎完全代码)从某人在这里关于 strcpy 。两者都给我一个分段错误。

char * strcpy(char * destination,const char * source) { while((* destination ++ = * source ++)!='\0'); return destination; }

此代码有什么问题?

char * a =hello cout< strcpy(a,Haha)<< endl;

解决方案

因此,当你调用 strcpy char a [] =hello; cout< strcpy(a,Haha)<< endl;

strcpy 函数,复制后,目标将指向字符串的结尾,需要返回字符串的开头

I tried to make strcpy myself. It should work, I even copied and pasted the (almost exact code) from someones post here about strcpy. Both give me a "Segmentation Fault".

char* strcpy(char * destination, const char * source) { while( (*destination++ = *source++) != '\0' ) ; return destination; }

What's wrong with this code?

char* a = "hello"; cout << strcpy(a, "Haha") << endl;

解决方案

You are trying to write to data segment since "hello" is stored there.

Therefore, when you call strcpy you get segmentation fault.

Try:

char a[] = "hello"; cout << strcpy(a, "Haha") << endl;

instead.

EDIT: Inside your strcpy function, after the copy, destination will point to end of the string, you need to return beginning of the string instead.

更多推荐

我的strcpy有什么问题?

本文发布于:2023-10-22 23:01:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   strcpy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!