1. 程式人生 > >php擴充套件開發筆記(9)sizeof 和 strlen 遇到空字元 '\0' 的問題

php擴充套件開發筆記(9)sizeof 和 strlen 遇到空字元 '\0' 的問題

看看下面程式碼的不同表現

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

 int main (int argc ,char **argv) {

     char h[] = {'a', 'b', 'c', '\0', 'd', 'e'};
     char *p = "abc\0defnghel"; // 注意 \0

     printf("sizeof(h) = %lu\n", (long) sizeof(h));
     printf("strlen(h) = %lu\n"
, (long) strlen(h)); printf("sizeof(p) = %lu\n", (long) sizeof(p)); // p 是指標 printf("strlen(p) = %lu\n", (long) strlen(p)); return 0; }

執行結果

sizeof(h) = 6
strlen(h) = 3
sizeof(p) = 8
strlen(p) = 3