C语言-strcpy
C语言-strcpy
char a[10] = "abcde";
char b[5] = "XYZ";
strcpy(a,b);
printf("%c",a[2]);
执行strcpy后,a的前4个元素被复制为b的元素,剩余部分保持不变
strcpy(a,b);
把b的值复制到a, b的元素个数小于a,则复制b的元素到a中,包括 \0
,
char a[10] = "abcde";
char b[5] = "XYZ";
定义之后
元素 | 值 |
---|---|
a[0] | ‘a’ |
a[1] | ‘b’ |
a[2] | ‘c’ |
a[3] | ‘d’ |
a[4] | ‘e’ |
a[5] | ‘\0’ |
b[0] | ‘X’ |
b[1] | ‘Y’ |
b[2] | ‘Z’ |
b[3] | ‘\0’ |
strcpy(a,b);
复制后
元素 | 值 |
---|---|
a[0] | ‘X’ |
a[1] | ‘Y’ |
a[2] | ‘Z’ |
a[3] | ‘\0’ |
a[4] | ‘e’ |
a[5] | ‘\0’ |
b[0] | ‘X’ |
b[1] | ‘Y’ |
b[2] | ‘Z’ |
b[3] | ‘\0’ |