C语言高级教程-C语言数组(二)

简介: C语言高级教程-C语言数组(二)

C语言高级教程-C语言数组(二)


e60e06b77cb34f37a3edcd557eaa06aa.jpg


本文的编译环境

本文的编译环境使用的是集成开发环境:Visual Studio 2019


e583c4cd01004969964d0c91d289bde1.png


Visual Studio 2019官网链接如下

Visual Studio 2019官网链接


5f3d0faf855a49e5b354fc888a2f32cf.png



Visual Studio 2019集成的开发环境的特点有


Visual Studio 2019默认安装Live Share代码协作服务。

帮助用户快速编写代码的新欢迎窗口、改进搜索功能、总体性能改进。

Visual Studio IntelliCode AI帮助。

更好的Python虚拟和Conda支持。

以及对包括WinForms和WPF在内的.NET Core 3.0项目支持等 。


一、前文:C语言数组(一)的链接


在C语言数组(一)的教程中,简单的介绍了如下的几点


介绍如何在C语言程序中使用数组。

后在编写程序使用数组时,如何通过一个名称来引用一组数值。

通过程序实例来掌握了C语言一维数组的定义,使用方法。

C语言数组(一)的文章链接如下所示

文章:C语言数组(一)


二、C语言数组的寻址


寻址运算符&输出其操作数的内存地址。


&:它广泛用于scanf函数。

它放在存储输入的变量名称之前,scanf()函 数就可以利用这个变量的地址,允许将键盘输入的数据存入变量。

只把这个变量名称用作函数的参数,函数就可以使用变量存储的值。

而把寻址运算符放在变量名称之前,函数就可以利用这个变量的地址。


2.1 使用寻址运算符"&"


下面的程序输出一些变量的地址


#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
请按任意键继续. . .

be46b8c03a5e471ba154f6dee66393a9.png

声明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;


2.2 输出变量地址的格式


下来输出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);

使用%u显示sizeof 生成的值,因为它是无符号的整数。

使用一个新的格式说明符%p,来输出变量的地址。

这个格式说明符指定输出一个内存地址,其值为十六进制。内存地址一般是32位或64位,地址的大小决定了可以引用的最大内存量。


输出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);
  • 事实上,程序本身不如输出那么有趣。
  • 看看显示出来的地址,地址值逐渐变小,是成“等差排列”,如下图所示。



image.png


  • 在本例中,地址b比a低4,c比b低4。这是因为每个long类型的变量占用4个字节。
  • 变量d、e、f也是如此,但它们的差是8。这是因为类型double的值用8个字节来存储。


三、输出数组里面变量的地址


3.1 输入数组里面存储的数据变量的地址


下面的程序将输出数组里面的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
请按任意键继续. . .

76360e2ac7d646aeb25577459c2899fc.png


  • 有地址的变化规律可以看出int类型的地址占四个字节。
  • 数组中存储的数据地址是呈现递增关系。


3.2 “&数组名”:代表数组首地址


&数组名:是取出数组的首个(第一个)数据的地址。

增加代码如下所示


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
请按任意键继续. . .

35c6540132d94e5b90d467b9da0d1fd7.png

  • 可以看出"&数组名"就是将数组中的第一个变量的地址取出来。

四、完整程序

本文的完整程序如下所示

4.1 Main.h 文件程序

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

4.2 Main.c 文件程序

#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语言数组寻址的应用。



01a715ecf7b443db8d52e35cd5dc81fb.jpg

本文到这里就结束啦。

希望本文的C语言数组寻址教程能对你有所帮助。


相关文章
|
2天前
|
存储 编译器 C语言
C语言之数组
C语言之数组
24 0
|
2天前
|
C语言
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
|
1天前
|
存储 C语言
C语言——数组(下)
C语言——数组(下)
4 0
C语言——数组(下)
|
1天前
|
C语言
C语言——数组(上)
C语言——数组(上)
6 0
|
2天前
|
C语言
每天一道C语言编程(数组操作)
每天一道C语言编程(数组操作)
6 0
|
2天前
|
C语言
每天一道C语言编程(第一弹~):数组
每天一道C语言编程(第一弹~):数组
9 0
|
2天前
|
机器学习/深度学习 C语言
C语言三维数组的创建
该代码片段展示了如何在C语言中创建一个动态的3D数组。由于`n`在编译时未知,不能直接声明为`int f[n][n][n]`。正确的方法是使用`malloc`进行动态内存分配。首先分配`n`个`int **`,然后对每一层分配`n`个`int *`,最后每个元素分配`n`个`int`。之后可以使用这个3D数组,并在完成后正确释放内存。
11 2
|
2天前
|
C语言
数组深入剖析(C语言基础入门)
数组深入剖析(C语言基础入门)
|
2天前
|
存储 C语言
C语言中字符串的引用与数组元素操作
C语言中字符串的引用与数组元素操作
23 0
|
2天前
|
存储 人工智能 程序员
爱上C语言:超详细讲解数组,学数组看这一篇就够了(数组篇)
爱上C语言:超详细讲解数组,学数组看这一篇就够了(数组篇)