年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2000|回复: 0

数组在内存中的存储情况

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-3-3 21:17:04 | 显示全部楼层 |阅读模式
    源代码:
    1. /*
    2. 1、分别定义int、double、char以及数组类型的变量;
    3. 2、分别取其地址进行比较;
    4. 3、各变量在内存中的存储情况;
    5. */
    6. #include <stdio.h>
    7. int main() {
    8.     int a;  //0x7fff52282c68
    9.    
    10.     double b;  //0x7fff52282c60
    11.    
    12.     char c;  //0x7fff52282c5f
    13.    
    14.     char d;  //0x7fff52282c5e
    15.    
    16.     char chars[6]; //0x7fff52282c58 ~ 0x7fff52282c5d 一共6个字节,
    17.    
    18.     printf("Address of a is:%p\n", &a);
    19.    
    20.     printf("Address of b is:%p\n", &b);
    21.    
    22.     printf("Address of c is:%p\n", &c);
    23.    
    24.     printf("Address of d is:%p\n", &d);
    25.    
    26.     printf("Address of chars is:%p\n", chars);
    27.    
    28.     for (int i = 0; i < 6; i++) {
    29.         
    30.         printf("Address of chars[%d] is:%p\n", i, &chars[i]);
    31.         
    32.     }
    33.     return 0;
    34. }
    复制代码
    输出结果为:
    1. Address of a is:0x7fff52282c68
    2. Address of b is:0x7fff52282c60
    3. Address of c is:0x7fff52282c5f
    4. Address of d is:0x7fff52282c5e
    5. Address of chars is:0x7fff52282c58
    6. Address of chars[0] is:0x7fff52282c58
    7. Address of chars[1] is:0x7fff52282c59
    8. Address of chars[2] is:0x7fff52282c5a
    9. Address of chars[3] is:0x7fff52282c5b
    10. Address of chars[4] is:0x7fff52282c5c
    11. Address of chars[5] is:0x7fff52282c5d
    复制代码
    内存示意图:
    QQ20140303-2@2x.png

    变量在内存中存储都是根据变量定义的顺序从高地址往低地址进行分配,但数组中的各变量是从低地址往高地址分配,从而第一个的地址即为数组的地址


    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-4 00:07 , Processed in 0.051055 second(s), 26 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表