社区
C语言
帖子详情
我的strcpy哪里错了?
predrag
2005-03-09 06:05:08
void mystrcpy (char* to, const char* from)
{
to = new char[mystrlen(from)+1];
for (; *from != 0; ++from, ++to)
{
*to = *from;
}
*to = 0;
}
运行时说一段内存不能读,正确的该怎么写?
...全文
182
6
打赏
收藏
我的strcpy哪里错了?
void mystrcpy (char* to, const char* from) { to = new char[mystrlen(from)+1]; for (; *from != 0; ++from, ++to) { *to = *from; } *to = 0; } 运行时说一段内存不能读,正确的该怎么写?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
pcboyxhy
2005-03-09
打赏
举报
回复
void mystrcpy (char* to, const char* from) //传的是 to的值,不是地址
{
to = new char[mystrlen(from)+1]; //改变不了调用函数的地方的 to的值
for (; *from != 0; ++from, ++to)
{
*to = *from;
}
*to = 0;
}
还是用传地址吧。
void foo(char *p)
{
p=new char;
}
char *s=new char;
foo(s); //执行后s的值没有改变
llf_hust
2005-03-09
打赏
举报
回复
#include<stdio.h>
#include<ctype.h>
#include<assert.h>
void mystrcpy (char* to, const char* from)
{
assert((to != NULL) && (from != NULL));
while( (*to++ = *from++) != '\0');
}
main()
{
char s[20];
char *p = "abcd";
clrscr();
mystrcpy(s,p);
puts(s);
getche();
}
我这个通过测试了
predrag
2005-03-09
打赏
举报
回复
还是不行啊,是不是我得main也错了:
int main()
{
const char* temp = "predrag";
char *target;
mystrcpy(target, temp);
cout << target << endl;
return 0;
}
avalonBBS
2005-03-09
打赏
举报
回复
const char * mystrcpy (char *to, const char *from)
{
char *temp=to;
for (; *from; ++from,++temp)
*temp = *from;
*temp = '\0';
return to;
}
int main()
{
char * a="abcdefg";
char *p = new char[ strlen(a)+1 ];
mystrcpy(p,a);
puts( p );
return 0;
}
llf_hust
2005-03-09
打赏
举报
回复
void mystrcpy (char* to, const char* from)
{
assert((to != NULL) && (from != NULL))
while( (*to++ = *from++) != '\0')
NULL;
}
wyystxl
2005-03-09
打赏
举报
回复
void mystrcpy (char* to, const char* from)
{
to = new char(strlen(from)+1);
while(!*from)
*to++ = *from++;
*to = '\0';
}
深入分析C中不安全的sprintf与
strcpy
经过跟踪调试, 发下不少 bug 源于 sprintf 和 ... 您可能感兴趣的文章:浅析C语言中printf(),sprintf(),scanf(),sscanf()的用法和区别基于C语言sprintf函数的深入理解C++中sprintf()函数的使用详解基于C++中sprintf的
错
关于C++中
strcpy
函数例题讲解
1.函数原型 char *
strcpy
(char *dest,const char *src) 2.从src地址开始且含有null结束符的字符串复制到以dest地址开始的字符串中,并返回指向dest的指针。通俗的讲就是将 src字符数组复制到dest数组中,如果dest...
总结C/C++面试中可能会碰到的字符串指针题
前言 不知道大家有没有这种体会,很多面试题看似简单,却需要深厚的基本功才能给出完美的解答。企业要求面试者写一个最简单的
strcpy
函数都可看出面试者在技术上究竟...找
错
题 试题1: 以下是引用片段: void test
c c++ 详解五题
详细解答了五道C C++题 没到都有生动的例子 值得一...1 简述
strcpy
、sprintf与memcpy的区别 2 怎样把一个单链表反序 3 short i = 0; i = i + 1L;这两句有
错
吗 4 简述指针常量与常量指针区别 5 谈谈你对面向对象的认识
几道华为经典C语言面试题
1、找
错
void test1() { char string[10]; char* str1="0123456789";
strcpy
(string, str1); } 这里string数组越界,因为字符串长度为10,还有一个结束符‘\0’。所以总共有11个字符长度。string数组大小为10,...
C语言
69,369
社区成员
243,082
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章