首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

能不能复制给栈指针解决方案

2012-02-24 
能不能复制给栈指针题目是把 abc 加到 abcdef 中的第三个字符变成 abcabcdef 代码如下:#includeios

能不能复制给栈指针
题目是把 "abc "加到 "abcdef "中的第三个字符   变成 "abcabcdef "
代码如下:
#include   <iostream.h>
#include   <malloc.h>
#include   <string.h>
void   insert(char   *dest,const   char   *src);
void   main()
{
char   dest[]= "abcdef ";
const   char   *src= "abc ";
insert(dest,src);
cout < <dest;

}

void   insert(char   *des,const   char   *src)
{
char   *dest_copy=des;
char   *p=(char   *)malloc(sizeof(char)*strlen(des)+1);//
char   *p_copy=p;//
int   len=strlen(des);//
for(int   i=0;i <len;i++)//复制dest到p
*p++=*des++;//
*p= '\0 ';//

//dest= "abcabc "
des=dest_copy+3;
while(*src!= '\0 ')
*des++=*src++;

//dest= "abcabcdef "
p=p_copy+3;
while(*p!= '\0 ')
*des++=*p++;
*des= '\0 ';
des=dest_copy;

}

最后提示出错:大概是指针的问题!

[解决办法]
什么错误?
[解决办法]
不好意思编译时insert(dest,src);没注释掉!~~仔细看过了函数没问题
0040546C mov dword ptr [mainret],eax
0040546F mov edx,dword ptr [mainret]
00405472 push edx
00405473 call exit (004080f0)
执行到了mov dword ptr [mainret],eax时堆栈溢出了!~~~没别的解决办法,只有把
数组大小改大点!~~~

热点排行