【问题标题】:Why does Dec memory address descend and Hex memory address ascend?为什么Dec内存地址下降而Hex内存地址上升?
【发布时间】:2018-10-17 07:28:21
【问题描述】:

我编写了一个 C 程序,从键盘输入 5 个 int/float/double 值并将它们存储在相应数据类型的数组中。

程序将一个表格写入屏幕,第一列包含十进制数组元素的内存地址,第二列包含十六进制数组元素的内存地址,第三列包含整数值内存地址。

在检查表时,我注意到 int 和 float 内存地址值减少了 4 个字节并加倍了 8,而十六进制增加了 4 和 8 个字节。

为什么Dec内存地址下降而Hex内存地址上升?

#include <stdio.h>
#define SIZE 5

main()
{
    int i;
    int intArray[SIZE];
    float floatArray[SIZE];
    double doubleArray[SIZE];

    int* ptrIntArray;
    float* ptrFloatArray;
    double* ptrDoubleArray;

    ///assign pointers to arrays to point to the start of each array
    ptrIntArray=intArray;
    ptrFloatArray=floatArray;
    ptrDoubleArray=doubleArray;

    printf("enter %d integers: \n", SIZE);
    for(i=0; i<SIZE; i++)
    {
        scanf("%d", &intArray[i]);
    }
    printf("enter %d floats: \n", SIZE);
    for(i=0; i<SIZE; i++)
    {
        scanf("%f", &floatArray[i]);
    }
    printf("enter %d doubles: \n", SIZE);
    for(i=0; i<SIZE; i++)
    {
        scanf("%lf", &doubleArray[i]);
    }

    printf("Memory address base 10\tMemory address base 16\tcontents at address\n");
    printf("Integers:\n\n");
    for(i=0; i<SIZE; i++)
    {
        printf("%d\t\t\t%x\t\t\t%d\n",ptrIntArray+i,ptrIntArray+i,*(ptrIntArray+i));
    }
    printf("\nFloats:\n\n");
    for(i=0; i<SIZE; i++)
    {
        printf("%d\t\t\t%x\t\t\t%f\n",ptrFloatArray+i,ptrFloatArray+i,*(ptrFloatArray+i));
    }
    printf("\nDoubles:\n\n");
    for(i=0; i<SIZE; i++)
    {
        printf("%d\t\t\t%x\t\t\t%lf\n",ptrDoubleArray+i,ptrDoubleArray+i,*(ptrDoubleArray+i));
    }
}

输出:

enter 5 integers: 
1
2
3
4
5
enter 5 floats: 
1.5
2.5
3.5
4.5
5.5
enter 5 doubles: 
1.55
2.55
3.55
4.55
5.55
Memory address base 10  Memory address base 16  contents at address
Integers:

-516081056          e13d3a60            1
-516081052          e13d3a64            2
-516081048          e13d3a68            3
-516081044          e13d3a6c            4
-516081040          e13d3a70            5

Floats:

-516081088          e13d3a40            1.500000
-516081084          e13d3a44            2.500000
-516081080          e13d3a48            3.500000
-516081076          e13d3a4c            4.500000
-516081072          e13d3a50            5.500000

Doubles:

-516081136          e13d3a10            1.550000
-516081128          e13d3a18            2.550000
-516081120          e13d3a20            3.550000
-516081112          e13d3a28            4.550000
-516081104          e13d3a30            5.550000

Process returned 0 (0x0)   execution time : 57.533 s
Press ENTER to continue.

【问题讨论】:

    标签: arrays pointers memory


    【解决方案1】:
    -516081056          e13d3a60
    -516081052          e13d3a64
    -516081048          e13d3a68
    -516081044          e13d3a6c
    -516081040          e13d3a70
    

    两组数字每次迭代都增加4。十进制数都是负数。使用%u 打印无符号十进制数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 2016-04-16
      • 2018-10-22
      • 2015-01-22
      • 1970-01-01
      • 2020-07-18
      相关资源
      最近更新 更多