急~~~c语言的问题

来源:百度知道 编辑:UC知道 时间:2024/07/01 01:35:03
#include <stdio.h>
#include <string.h>
void main()
{ char str[3][4]={"abc",'d','e','f','g',"xyz"};
strcpy(&str[0][2],str[1]);
printf("%d",strlen(str[0]));
}
答案是9,希望有过程

明白你的疑惑了 我把你程序加了几行 先看看吧
#include <stdio.h>
#include <string.h>
void main()
{ char str[3][4]={"abc",'d','e','f','g',"xyz"};

printf("%s\n",str[0]);
printf("%s\n",str[1]);
printf("%s\n",str[2]);

strcpy(&str[0][2],str[1]);
printf("%d",strlen(str[0]));
getch();

}

输出如下
abc
defgxyz
xyz
9

这个数组中 存的 数据如下
a b c \0
d e f g
x y z \0

你可以看到我的 str[1]打印输出的字符是defgxyz没错吧
那么这行代码strcpy(&str[0][2],str[1]);执行后会怎样?
是把 d e f g x y z \0 这8个字符复制给str[0][2]这个地址后的8的字节存储空间上
既把d e f g x y z \0 复制到字母b后面的8个字节也就是c \0 d e f g x y这时的内存空间变成这样

a b d e
f g x y
z \0 z \0
最后一条语句
printf("%d",strlen(str[0]));
输出str[0]字符串的长度,长度是到第一个 \0 结束 ,你数一下从str[0]开始