Sian 发表于 2014-3-3 21:17:04

数组在内存中的存储情况

源代码:
/*
1、分别定义int、double、char以及数组类型的变量;
2、分别取其地址进行比较;
3、各变量在内存中的存储情况;
*/

#include <stdio.h>

int main() {

    int a;//0x7fff52282c68
   
    double b;//0x7fff52282c60
   
    char c;//0x7fff52282c5f
   
    char d;//0x7fff52282c5e
   
    char chars; //0x7fff52282c58 ~ 0x7fff52282c5d 一共6个字节,
   
    printf("Address of a is:%p\n", &a);
   
    printf("Address of b is:%p\n", &b);
   
    printf("Address of c is:%p\n", &c);
   
    printf("Address of d is:%p\n", &d);
   
    printf("Address of chars is:%p\n", chars);
   
    for (int i = 0; i < 6; i++) {
      
      printf("Address of chars[%d] is:%p\n", i, &chars);
      
    }

    return 0;
}输出结果为:Address of a is:0x7fff52282c68
Address of b is:0x7fff52282c60
Address of c is:0x7fff52282c5f
Address of d is:0x7fff52282c5e
Address of chars is:0x7fff52282c58
Address of chars is:0x7fff52282c58
Address of chars is:0x7fff52282c59
Address of chars is:0x7fff52282c5a
Address of chars is:0x7fff52282c5b
Address of chars is:0x7fff52282c5c
Address of chars is:0x7fff52282c5d内存示意图:


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


页: [1]
查看完整版本: 数组在内存中的存储情况