TA的每日心情 | 汗 2024-10-15 10:05 |
---|
签到天数: 372 天 [LV.9]以坛为家II
|
源代码:- /*
- 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[6]; //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[i]);
-
- }
- 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[0] is:0x7fff52282c58
- Address of chars[1] is:0x7fff52282c59
- Address of chars[2] is:0x7fff52282c5a
- Address of chars[3] is:0x7fff52282c5b
- Address of chars[4] is:0x7fff52282c5c
- Address of chars[5] is:0x7fff52282c5d
复制代码 内存示意图:
变量在内存中存储都是根据变量定义的顺序从高地址往低地址进行分配,但数组中的各变量是从低地址往高地址分配,从而第一个的地址即为数组的地址;
|
|