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



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

    在这里插入图片描述

    本文的编译环境

    本文的编译环境使用的是集成开发环境:Visual Studio 2019
    在这里插入图片描述

    Visual Studio 2019官网链接如下

    Visual Studio 2019官网链接
    在这里插入图片描述

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    按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
    请按任意键继续. . .
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    声明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;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 使用%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);
    
    • 1
    • 2
    • 3
    • 4
    • 事实上,程序本身不如输出那么有趣。

    • 看看显示出来的地址,地址值逐渐变小,是成“等差排列”,如下图所示。
      在这里插入图片描述

    • 在本例中,地址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");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    按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
    
    请按任意键继续. . .
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

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

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

    &数组名:是取出数组的首个(第一个)数据的地址。
    增加代码如下所示

    printf("The array of the address is %p\n", &arrays);
    
    • 1

    按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
    请按任意键继续. . .
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

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

    四、完整程序

    本文的完整程序如下所示

    4.1 Main.h 文件程序

    #pragma once
    
    #include 
    #include 
    #include 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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;
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    五、总结

    • 本文主要介绍了C语言高级编程的数组的寻址方法。
    • 通过几个实例程序来掌握C语言数组寻址的应用。

    在这里插入图片描述

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

  • 相关阅读:
    TensorFlow.NET--数据类型与张量详解
    java关键字
    (十一)React Ant Design Pro + .Net5 WebApi:后端环境搭建-IdentityServer4(三)持久化
    每日一题 2258. 逃离火灾(手撕困难!!!)
    Windows下PostgreSQL编译调试笔记
    Centos7 gcc/g++安装以及运行程序
    Git_回退到上一次commit与pull
    Selenium 模拟操作与 pytest 断言的结合使用
    中国三氧化二砷行业研究与未来预测报告(2022版)
    java计算机毕业设计教学质量评价系统源程序+mysql+系统+lw文档+远程调试
  • 原文地址:https://blog.csdn.net/m0_47419053/article/details/126608512