代码
#include <stdio.h>
#include <string.h>
int main(){
char str[20];
printf("char str[20];\nstrlen = %d\nsizeof = %d\nstr = %s\n\n", strlen(str), sizeof(str), str);
memset(str, 0, sizeof(str));
printf("memset(str, 0, sizeof(str));\nstrlen = %d\nsizeof = %d\nstr = %s\n\n", strlen(str), sizeof(str), str);
strcpy(str, "Hello World");
printf("strcpy(str, \"Hello World\");\nstrlen = %d\nsizeof = %d\nstr = %s\n\n", strlen(str), sizeof(str), str);
return 0;
}
输出
char str[20];
strlen = 8
sizeof = 20
str =
memset(str, 0, sizeof(str));
strlen = 0
sizeof = 20
str =
strcpy(str, "Hello World");
strlen = 11
sizeof = 20
str = Hello World
其中定义后str的内容为其应为内存中原本的内容