一:(2012年03月19日) strlen 与sizeof
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[9] = "hello";
char str2[] = "hello";
char *str3 = "hello";
char *str4 = "";
printf("strlen(str1) is %u\n", strlen(str1));
printf("sizeof(str1) is %u\n", sizeof(str1));
printf("strlen(str2) is %u\n", strlen(str2));
printf("sizeof(str2) is %u\n", sizeof(str2));
printf("strlen(str3) is %u\n", strlen(str3));
printf("sizeof(str3) is %u\n", sizeof(str3));
printf("strlen(str4) is %u\n", strlen(str4));
printf("sizeof(str4) is %u\n", sizeof(str4));
printf("sizeof(\"\") is %u\n", sizeof(""));
printf("sizeof(\"hello\") is %u\n", sizeof("hello"));
return 0;
}
运行结果:(程序在64位系统下运行)
strlen(str1) is 5
sizeof(str1) is 9
strlen(str2) is 5
sizeof(str2) is 6
strlen(str3) is 5
sizeof(str3) is 8
strlen(str4) is 0
sizeof(str4) is 8
sizeof("") is 1
sizeof("hello") is 6