指针访问数组元素

#include <stdio.h>

int *p = (int[]){8, 7}; // creates an unnamed static array of type int[2]
                        // initializes the array to the values {2, 4}
                        // creates pointer p to point at the first element of the array

void main(void)
{
    int n = 99, *p = &n;

    // 此时 *p == 99;
    printf("n: %d\n", *p);

    p = (int [2]){*p}; // creates an unnamed automatic array of type int[2]
                       // initializes the first element to the value formerly held in *p
                       // initializes the second element to zero
                       // stores the address of the first element in p

    // 此时 数组为 [99, 0]  p指向数组中的首元素(99)

    // 数组指针访问数组元素,只要在 地址不断加1 即向下移动一个单元
    for (int i = 0; i < 2; i++)
    {
        printf("array %d %d\n", i, *(p + i));
    }

}

 

posted @ 2021-10-28 18:00  Joynic  阅读(190)  评论(0编辑  收藏  举报