谭浩强书中一例题的疑问
谭书10.4.2节中例10.20用函数调用实现字符串的复制
#include<stdio.h>
void copy_string(char *from, char *to)
{
int i = 0;
while(*(from+i) != '\0'){
*(to+i) = *(from+i);
i++;
}
*(to+i) = '\0';
}
void main()
{
char *a = "I am a teacher.";
char *b = "you are a student.";
printf("string a = %s\nstring b = %s\n", a, b);
copy_string(a, b);
printf("\nstring a = %s\nstring b = %s", a, b);
getch();
}
我在VIUAL C++ 6.0中编译通过,但运行出现问题,只执行了第一个打印函数.并出现错误信息对话框:"example10_20.exe 遇到问题需要关闭。我们对此引起的不便表示抱歉。"
但相同的代码在TC2.0下可以正常运行.
在VIUAL C++ 6.0中如果将char *a = "I am a teacher.";char *b = "you are a student.";改为char a[] = "I am a teacher."; char b[] = "you are a student.";则可以正常运行.
请大家帮忙分析下怎么回事?