65.C指標---sizeof()函式和strlen()函式常見考

不爱美女爱辣条發表於2024-10-20
#include <stdio.h>
#include <string.h>

int main() {
    int a[] = {1, 2, 3, 4};
    printf("%d\n", sizeof(a));
    printf("%d\n", sizeof(a + 0));
    printf("%d\n", sizeof(*a));
    printf("%d\n", sizeof(a + 1));
    printf("%d\n", sizeof(a[1]));
    printf("%d\n", sizeof(&a));
    printf("%d\n", sizeof(*&a));
    printf("%d\n", sizeof(&a + 1));
    printf("%d\n", sizeof(&a[0]));
    printf("%d\n", sizeof(&a[0] + 1));
    printf("=============================\n");
    char arr[] = {'a', 'b', 'c', 'd', 'e', 'f'};
    printf("%d\n", sizeof(arr));
    printf("%d\n", sizeof(arr + 0));
    printf("%d\n", sizeof(*arr));
    printf("%d\n", sizeof(arr[1]));
    printf("%d\n", sizeof(&arr));
    printf("%d\n", sizeof(&arr + 1));
    printf("%d\n", sizeof(&arr[0] + 1)); 
    printf("=============================\n");
    printf("%d\n", strlen(arr)); 
    printf("%d\n", strlen(arr + 0)); 
    printf("%d\n", strlen(*arr)); 
    printf("%d\n", strlen(arr[1])); 
    printf("%d\n", strlen(&arr)); 
    printf("%d\n", strlen(&arr + 1)); 
    printf("%d\n", strlen(&arr[0] + 1)); 
    printf("=============================\n");
    char b[] = "abcdef"; 
    printf("%d\n", sizeof(b));
    printf("%d\n", sizeof(b + 0));
    printf("%d\n", sizeof(*b)); 
    printf("%d\n", sizeof(b[1]));
    printf("%d\n", sizeof(&b)); 
    printf("%d\n", sizeof(&b + 1)); 
    printf("%d\n", sizeof(&b[0] + 1)); 
    printf("=============================\n");
    printf("%d\n", strlen(b)); 
    printf("%d\n", strlen(b + 0)); 
    printf("%d\n", strlen(*b));
    printf("%d\n", strlen(b[1]));
    printf("%d\n", strlen(&b)); 
    printf("%d\n", strlen(&b + 1)); 
    printf("%d\n", strlen(&b[0] + 1)); 
    printf("=============================\n");
    char *p = "abcdef";
    printf("%d\n", sizeof(p));
    printf("%d\n", sizeof(p + 1)); 
    printf("%d\n", sizeof(*p)); 
    printf("%d\n", sizeof(p[0])); 
    printf("%d\n", sizeof(&p));
    printf("%d\n", sizeof(&p + 1));
    printf("%d\n", sizeof(&p[0] + 1));
    printf("=============================\n");
    printf("%d\n", strlen(p));
    printf("%d\n", strlen(p + 1));
    printf("%d\n", strlen(*p));
    printf("%d\n", strlen(p[0]));
    printf("%d\n", strlen(&p));
    printf("%d\n", strlen(&p + 1));
    printf("%d\n", strlen(&p[0] + 1));

}

相關文章