指针系列(二)


//指针与数组

char str[120];
    printf("please input a string:\n");
    scanf_s("%s\n", str);
    printf("your str is :%p\n", str);
    printf("your str is :%p\n", &str[0]);

    char a[] = "fish";
    int b[5] = { 1,2,3,4,5 };
    float c[5] = { 1.1,2.2,3.3,4.4,5.5 };
    double d[5] = { 1.1,2.2,3.3,4.4,5.5 };


    printf("a[0] = %p,a[1] = %p,a[2] = %p.a[4]=%p\n", &a[0], &a[1], &a[2], &a[3]);
    printf("b[0] = %p,b[1] = %p,b[2] = %p.b[4]=%p\n", &b[0], &b[1], &b[2], &b[3]);
    printf("c[0] = %p,c[1] = %p,c[2] = %p.c[4]=%p\n", &c[0], &c[1], &c[2], &c[3]);
    printf("d[0] = %p,d[1] = %p,d[2] = %p.d[4]=%p\n", &d[0], &d[1], &d[2], &d[3]);

// 指針的運算與讀取
    char *p = a;
    int *p2 = b;
    printf("*p = %c,*(p+1)=%c,*(p+2)=%c\n", *p, *(p + 1), *(p + 2));
    printf("*p2 = %d,*(p2+1)=%d,*(p2+2)=%d\n", *p2, *(p2 + 1), *(p2 + 2));

//定义字符指针变量

    const char *str = "I Love C !";
    int i, length;
    length = strlen(str);

    for ( i = 0; i < length; i++)
    {
        printf("%c", str[i]);

    }
    printf("\n");

C