ladies and gentlemen,please give a hand,a powerful hand!
#include<stdio.h>int main(){
void copy_string(char from[],char to[]);
char a[]="I am a teacher.";
char b[]="You are a student.";
int i;
printf("string a is:%s\nstring b is:%s\n",a,b);
printf("copy string b to string a!\n");
copy_string(b,a);
printf("string a is:%s\nstring b is:%s\n",a,b);
printf("sizeof(a)=%d\nstrlen(a)=%d\n",sizeof(a),strlen(a));
putchar('\n');
for(i=0;i<=18;i++)
printf("%c",a[i]);
putchar('\n');
return 0;
}
void copy_string(char from[],char to[]){
int i=0;
while(from[i]!='\0')
{
to[i]=from[i];
i++;
}
to[i]='\0';
}
结果输出如下:
string a is:I am a teacher.
string b is:You are a student.
copy string b to string a!
string a is:You are a student.
string b is:You are a student.
sizeof(a)=16
strlen(a)=18
You are a student.
只有一个疑问:字符数组a在调用copy_string函数复制b数组后输出的字符串长度为18,但是所占字节数却还是16,各位可以看一下代码,便可知口齿不清的小弟所要表达的意思,问一句,这是为何,数组a 主函数调用copy_string函数前后的存储状况内容!