VS2008写C程序,遇到错误,求帮助

清天灵月 2012-09-28 07:56:06
遇到了个小问题,求大神解答,我用得是VS2008,写的是C的程序!
#include <stdio.h>
#include <assert.h>

char *mystrcpy(char *strDestination, const char *strSource)
{
char *strD = strDestination;
assert(strDestination!=NULL && strSource!=NULL);
while (1)
{
char temp;
temp = *strSource;
*strDestination = temp; //程序运行到这里就弹出,中断
strDestination++;
strSource++;
if (temp == '\0') break;
}
return strD;
}

void mystrcat(char* str1,char* str2)
{
while(*str1!='\0') str1++;
while(*str2!='\0')
{
*str1=*str2; //运行到这里也是报错
str1++;
str2++;
}
*str1='\0';
}
void mystrcat2(char* str1,char* str2)
{
int i=0,j=0;
while(str1[i]!='\0')i++;
while(str2[j]!='\0')
{
str1[i] = str2[j]; //尝试另一种写法,又是这里错,怎么回事?大神指导下!
i++;
j++;
}
str1[i]='\0';
}
void main()
{
char *str1 = "ah";
char *str2 = "chinacyr";
//mystrcpy(str1,str2);
//mystrcat(str1,str2);
}
...全文
113 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
清天灵月 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

百度百科。。。。
http://blog.csdn.net/luckyaslan/article/details/7742335
[/Quote]
谢谢你这么热心!
左眼看到鬼 2012-09-28
  • 打赏
  • 举报
回复
百度百科。。。。
http://blog.csdn.net/luckyaslan/article/details/7742335
左眼看到鬼 2012-09-28
  • 打赏
  • 举报
回复
C语言标准库函数
  原型声明:extern char *strcpy(char *dest,const char *src);
  头文件:string.h
  功能:把从src地址开始且含有NULL结束符的字符串赋值到以dest开始的地址空间
  说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
  返回指向dest的指针。

摘自百度知道
清天灵月 2012-09-28
  • 打赏
  • 举报
回复
char *p1;
char *str1 = "ah";
char *str2 = "chinacyr";
char source1[50]="ah";
char source2[20]="chinacyr";
p1 = (char*)malloc(20);
mystrcpy(p1,str1);
puts(source1);
system("pause");
我这样写就对了,奇怪。同样是指向一块内存地址的指针,为何是两种结果?求指导?
清天灵月 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

亲,常量是什么?
[/Quote]
常量:指在程序运行过程中,其值不可改变的量.与变量不同,常量没有名称,由于常量同样要存储,所起其有地址.
char *p="123456";//123456\0 在常量区,p3在栈上

如果是 char *p2;
p2= (char*)malloc(20);
然后 mystrcpy(p2,p); 会如何?
清天灵月 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

亲,常量是什么?
[/Quote]
常量:指在程序运行过程中,其值不可改变的量.与变量不同,常量没有名称,由于常量同样要存储,所起其有地址.
char *p="123456";//123456\0 在常量区,p3在栈上

如果是 char *p2;
p2= (char*)malloc(20);
然后 mystrcpy(p2,p); 会如何?
大熊猫侯佩 2012-09-28
  • 打赏
  • 举报
回复
亲,常量是什么?
清天灵月 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

char *str1 = "ah";
你这样定义,str1是一个指向字符串常量的指针。
str1你进行strcpy或者strcat的时候是需要有足够的空间来进行数据存储的。
你可以定义成 char str1[100]="ah";
[/Quote]
还是不太懂?不过,我刚试了下字符数组,确实没问题了!谢了!
左眼看到鬼 2012-09-28
  • 打赏
  • 举报
回复
char *str1 = "ah";
你这样定义,str1是一个指向字符串常量的指针。
str1你进行strcpy或者strcat的时候是需要有足够的空间来进行数据存储的。
你可以定义成 char str1[100]="ah";

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧