#include <stdio.h>
int main()
{
void copy_string(char *from,char *to);
char *a="I am a teacher.";
printf("%d\n",sizeof(a));
char b[]="You are a student.";
char *p=b;
printf("string a=%s\nstring b=%s\n",a,b);
printf("\ncopy string a to string b:\n");
copy_string(a,p);
printf("string a=%s\nstring b=%s\n",a,b);
return 0;
}
void copy_string(char *from,char *to)
{
while(*to++=*from++);
}
昨天我吃完饭后去上课,想了一下这个问题。今天我知道是怎么回事了。就是第六行的这句的问题。printf("%d\n",sizeof(a));其实,这是书上的原代码。这句是我自己加的。我想测试一下编译环境给a这个指针变量分配多少个字节。其实在code::blocks中是可以将这个语句放在一些定义的变量或常量前的,但在微软的编译系统中不行。如果将printf("%d\n",sizeof(a));这句放在char *p=b;的后面的行中就没问题了。可能是不同的编译系统编译方式和要求不同吧。
这代码是没错的。看我补充的就知道了。
对啊,,哈哈,其实是微软那个编译的环境有问题。