谁能解释下
											#include<iostream>
using namespace std;
int main()
{
    int top=0;
       char ch,*p;
    p=new char;
    while(ch!='\n')
    {
    ch=getchar();
    
    p[top]=ch;
    top++;
    
    };
       cout<<p;
   return 0;
}
如果我输入"abcd",我认为应当输出也是"abcd",但怎么输出"abcd"后后面还有一段乱码?
#include<iostream>
using namespace std;
int main()
{
    int top=0;
       char ch,*p;
    p=new char;
    while(ch!='\n')
    {
    ch=getchar();
    
    p[top]=ch;
    top++;
    
    };
    p[top]='\0';
       cout<<p;
   return 0;
}
如果改成这样,输出的字符串后面还有一个换行符,如果改成p[top-1]='\0';就正确了.
我自己试出来的,但我不知道原因,谁能解释下



											
	    

	

