cout << "please enter a integer" << endl;
int num;
cin >> num;
char *A=(char *)malloc(sizeof(char));
int length = strlen(A);
char *test = A;
char *B = "heel";
while (num){
*test = num % 10 + '0';
num = num / 10;
*test++;
}
*test++ = '\0';
cout << endl << A<<endl<<length<<endl<<B;
为什么对test操作结果A输出相应的结果而test中是乱码?
因为test指针你已经进行了++等操作,它已经指向末尾地址了,所以打印的时候是乱码了,而A是开始分配的地址空间,没有动。所以正确。