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

加长数组这样可以吗解决方案

2012-02-10 
加长数组这样可以吗#includestdio.h#includestring.hintmain(void){charsrc[]*********************

加长数组这样可以吗
#include   <stdio.h>  
#include   <string.h>  
int   main(void)  
{  
      char   src[]   =   "************************************************************* ";  
      char   dest[]   =   "abcdefghijlkmnopqrstuvwxyz0123456709 ";  
      void   *ptr;  
      printf( "destination   before   memcpy:   %s\n ",   dest);  
      ptr   =   memcpy(dest+strlen(dest),   src,   strlen(src));  
      if   (ptr)  
            printf( "destination   after   memcpy:     %s\n ",   dest);  
      else  
            printf( "memcpy   failed\n ");  
      return   0;  
}

[解决办法]
const char* h1 = "Hi,ddddwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww ";
const char* h2 = "sean!!ddddddddddddddddddddddddddddddddd ";

int size = strlen(h1)+strlen(h2)+1;
char* h3 = (char*)malloc(size*sizeof(char));
char* h4 = h3;

strcpy_s(h4,size,h1);
h4 = h4 + strlen(h1);
strcpy_s(h4,size-strlen(h1),h2);

cout < < "h3 length = " < < strlen(h3) < < endl;
cout < < "h3 = " < < h3 < < endl;

free(h3);

热点排行