易百教程

编写程序以显示数组中每个元素的地址及其值

编写一个程序,用来显示数组中每个元素的地址及其值。

提示

应该看到每个地址由4个字节分隔。

参考实现代码

#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, numbers[x], pn);
        pn++;
    }
    system("pause");
    return(0);
}

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

numbers[1] = 1, address 00EFF7BC
numbers[2] = 2, address 00EFF7C0
numbers[3] = 3, address 00EFF7C4
numbers[4] = 4, address 00EFF7C8
numbers[5] = 5, address 00EFF7CC
numbers[6] = 6, address 00EFF7D0
numbers[7] = 7, address 00EFF7D4
numbers[8] = 8, address 00EFF7D8
numbers[9] = 9, address 00EFF7DC
numbers[10] = 10, address 00EFF7E0