易百教程

编写程序以使用点输出数组元素值

编写一个程序,使用点输出数组元素值。

实现代码

#include <stdio.h>

int main()
{
    int numbers[10];
    int x;
    int *pn;

    pn = numbers;       /* 初始化指针 */

    /* 填充数组 */
    for (x = 0; x < 10; x++)
    {
        *pn = x + 1;
        pn++;
    }

    pn = numbers;

    /* 显示数组 */
    for (x = 0; x < 10; x++)
    {
        printf("numbers[%d] = %d, address = %p\n",
            x + 1, *pn, pn);
        pn++;
    }
    system("pause");
    return(0);
}

执行上面示例代码,得到以下结果:

numbers[1] = 1, address = 008FF9BC
numbers[2] = 2, address = 008FF9C0
numbers[3] = 3, address = 008FF9C4
numbers[4] = 4, address = 008FF9C8
numbers[5] = 5, address = 008FF9CC
numbers[6] = 6, address = 008FF9D0
numbers[7] = 7, address = 008FF9D4
numbers[8] = 8, address = 008FF9D8
numbers[9] = 9, address = 008FF9DC
numbers[10] = 10, address = 008FF9E0