php擴充套件開發筆記(9)sizeof 和 strlen 遇到空字元 '\0' 的問題
阿新 • • 發佈:2018-12-30
看看下面程式碼的不同表現
#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