
本文的编译环境使用的是集成开发环境:Visual Studio 2019
Visual Studio 2019官网链接如下
Visual Studio 2019集成的开发环境的特点有
在C语言数组(一)的教程中,简单的介绍了如下的几点
C语言数组(一)的文章链接如下所示
寻址运算符&输出其操作数的内存地址。
&:它广泛用于scanf函数。
它放在存储输入的变量名称之前,scanf()函 数就可以利用这个变量的地址,允许将键盘输入的数据存入变量。
只把这个变量名称用作函数的参数,函数就可以使用变量存储的值。
而把寻址运算符放在变量名称之前,函数就可以利用这个变量的地址。
下面的程序输出一些变量的地址
#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
int main()
{
system("color 3E");
// Define some integer variables
long a = 1L;
long b = 2L;
long c = 3L;
// Define some floating-point variables
double d = 4.0;
double e = 5.0;
double f = 6.0;
printf("A variable of type long occupies %u bytes.", sizeof(long));
printf("\nHere are the addresses of some variables of type long:");
printf("\nThe address of a is: %p The address of b is: %p", &a, &b);
printf("\nThe address of c is: %p", &c);
printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));
printf("\nHere are the addresses of some variables of type double:");
printf("\nThe address of d is: %p The address of e is: %p", &d, &e);
printf("\nThe address of f is: %p\n", &f);
system("pause");
return 0;
}
按F5进行编译,运行结果如下所示
A variable of type long occupies 4 bytes.
Here are the addresses of some variables of type long:
The address of a is: 00EFFC60 The address of b is: 00EFFC54
The address of c is: 00EFFC48
A variable of type double occupies 8 bytes.
Here are the addresses of some variables of type double:
The address of d is: 00EFFC38 The address of e is: 00EFFC28
The address of f is: 00EFFC18
请按任意键继续. . .

声明3个long类型的变量和3个double类型的变量
// Define some integer variables
long a = 1L;
long b = 2L;
long c = 3L;
// Define some floating-point variables
double d = 4.0;
double e = 5.0;
double f = 6.0;
下来输出long变量占用的字节数,跟着输出这3个变量的地址
printf("A variable of type long occupies %u bytes.", sizeof(long));
printf("\nHere are the addresses of some variables of type long:");
printf("\nThe address of a is: %p The address of b is: %p", &a, &b);
printf("\nThe address of c is: %p", &c);
输出double变量占用的字节数,接着输出这3个变量的地址
printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));
printf("\nHere are the addresses of some variables of type double:");
printf("\nThe address of d is: %p The address of e is: %p", &d, &e);
printf("\nThe address of f is: %p\n", &f);
事实上,程序本身不如输出那么有趣。
看看显示出来的地址,地址值逐渐变小,是成“等差排列”,如下图所示。

在本例中,地址b比a低4,c比b低4。这是因为每个long类型的变量占用4个字节。
变量d、e、f也是如此,但它们的差是8。这是因为类型double的值用8个字节来存储。
下面的程序将输出数组里面的10变量的地址
int arrays[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
arrays[i] = rand() % 20 + 1;
}
for (int i = 0; i < 10; i++)
{
printf("%d of the address--> %p\n", arrays[i], &arrays[i]);
}
printf("\n");
按F5进行编译,运行结果如下所示
18 of the address--> 010FFE58
17 of the address--> 010FFE5C
4 of the address--> 010FFE60
15 of the address--> 010FFE64
6 of the address--> 010FFE68
12 of the address--> 010FFE6C
9 of the address--> 010FFE70
3 of the address--> 010FFE74
7 of the address--> 010FFE78
13 of the address--> 010FFE7C
请按任意键继续. . .

&数组名:是取出数组的首个(第一个)数据的地址。
增加代码如下所示
printf("The array of the address is %p\n", &arrays);
按F5进行编译,运行结果如下所示
13 of the address--> 006FFAC8
18 of the address--> 006FFACC
9 of the address--> 006FFAD0
18 of the address--> 006FFAD4
19 of the address--> 006FFAD8
16 of the address--> 006FFADC
17 of the address--> 006FFAE0
7 of the address--> 006FFAE4
17 of the address--> 006FFAE8
17 of the address--> 006FFAEC
The array of the address is 006FFAC8
请按任意键继续. . .

本文的完整程序如下所示
#pragma once
#include
#include
#include
#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
int main()
{
system("color 3E");
/* Define some integer variables
long a = 1L;
long b = 2L;
long c = 3L;
Define some floating-point variables
double d = 4.0;
double e = 5.0;
double f = 6.0;
int arrays[10];
printf("A variable of type long occupies %u bytes.", sizeof(long));
printf("\nHere are the addresses of some variables of type long:");
printf("\nThe address of a is: %p The address of b is: %p", &a, &b);
printf("\nThe address of c is: %p", &c);
printf("\n\nA variable of type double occupies %u bytes.", sizeof(double));
printf("\nHere are the addresses of some variables of type double:");
printf("\nThe address of d is: %p The address of e is: %p", &d, &e);
printf("\nThe address of f is: %p\n", &f);*/
int arrays[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
arrays[i] = rand() % 20 + 1;
}
for (int i = 0; i < 10; i++)
{
printf("%d of the address--> %p\n", arrays[i], &arrays[i]);
}
printf("\n");
printf("The array of the address is %p\n", &arrays);
system("pause");
return 0;
}
- 本文主要介绍了C语言高级编程的数组的寻址方法。
- 通过几个实例程序来掌握C语言数组寻址的应用。

本文到这里就结束啦。
希望本文的C语言数组寻址教程能对你有所帮助。