注册 登录
编程论坛 C++教室

简单的字符指针复制 怎么 不出现结果

honghong88 发布于 2011-11-11 21:38, 712 次点击
#include<iostream>
using namespace std;
#include< string>
#define  NULL 0
int main()
{
    char  stra[20]="hai",strb[20] ,   *str1 ,*str2;

      str1= stra ;
      str2=strb;
   for(;*str1!='\0';str1++,str2++  )
               
             *str2=*str1;
   *str2='\0';
   
   cout<<str1<<""<<str2;





简单的字符指针复制 怎么 不出现结果
3 回复
#2
lwei2011-11-12 11:00
你的指针循环前指向字串头,循环后知道后面了,当然没有输出内容。
#3
hxcet2011-11-18 13:35
LS正解。。。其实你这个程序是没必要用for循环的。
这样就可以。
#include<iostream>
using namespace std;
#include< string>
#define  NULL 0
void main()
{
    char  stra[20]="hai",strb[20] ,   *str1 ,*str2;

      str1= stra ;
      str2=strb;
      str2=str1;
      cout<<str1<<""<<str2;
}
#4
jj74125302011-11-22 00:36
2楼正解   只有指针指向字符串头部才可以输出
1