C语言高级教程-C语言数组(六):变长数组

简介: C语言高级教程-C语言数组(六):变长数组

image.jpeg

一、本文的编译环境


本文的编译环境使用的是集成开发环境: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项目支持等 。

前面文章的所有数组都在代码中指定了固定的长度。也可以定义其长度在程序运行期间确定的数组。下面是一个示例:


二、一维数组在执行期间确定长度

size_t size = 0;
printf ("Enter the number of elements you want to store: ") ;
scanf ("%zd", &size) ;
float values[size] ;


在这段代码中,把从键盘上读取的一一个值放在size中。接着使用size的值指定数组array的长度。

因为size_t是用实现代码定义的整数类型,所以如果尝试使用%d读取这个值,就会得到一个编译错误。

%zd中的z告诉编译器,它应用于size_t, 所以无论整数类型size_t是什么,编译器都会使说明符适用于读取操作。


三、二维数组在执行期间确定长度

还可以在执行期间确定二维或多维数组中的任意维或所有维。

例如:

size_ t rows = 0;
size t columns = 0;
printf ("Enter the number of rows you want to store: ") ;
scanf ("%zd",&rows) ;
printf ("Enter the number of columns in a row: ") ;
scanf ("%zd",&columns) ;
float beans [ rows] [columns] ;


  • 这里从键盘读取二维数组中的两个维。
  • 这两个数组维都在执行期间确定。

四、一维变长数组实例

一维变长数组实例如下所示

  • 在下面的程序中,一维变长数组是可以用的。


  size_t nGrades = 10;                    // Number of grades
    printf("Enter the number of grades: ");
    scanf("%zd", &nGrades);
    int grades[nGrades];                         // Array storing nGrades values
    long sum = 0L;                               // Sum of the numbers
    float average = 0.0f;                        // Average of the numbers
    printf("\nEnter the %u grades:\n", nGrades); // Prompt for the input
    // Read the ten numbers to be averaged
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("%2zd> ", i + 1);
        scanf("%d", &grades[i]);                   // Read a grade
        sum += grades[i];                          // Add it to sum
    }
    printf("The grades you entered are:\n");
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("Grade[%2zd] = %3d  ", i + 1, grades[i]);
        if ((i + 1) % 5 == 0)                         // After 5 values
            printf("\n");                            // Go to a new line                    
    }
    average = (float)sum / nGrades;                // Calculate the average
    printf("\nAverage of the %zd grades entered is: %.2f\n", nGrades, average);

下面是一些输出:

Enter the number of grades: 12
Enter the 12 grades:
1> 56
2> 67
3> 78
4> 67
5> 68
6> 56
7> 88
8> 98
9> 76
10> 75
11> 87
12> 72
The grades you entered are:
Grade[1]=56Grade[2]=67Grade[3]=78Grade[4]=67Grade[5]=68
Grade[6]=56Grade[7]=88Grade[8]=98Grade[9]=76Grade[10]=75
Grade[11] = 87 Grade[12] = 72
Average of the 12 grades entered is: 74.00


本例定义了一个变量nGrades来存储要输入的分数个数,并从键盘上读取数值:

  size_t nGrades = 10;                    // Number of grades
    printf("Enter the number of grades: ");
    scanf("%zd", &nGrades);


  • 再使用读入nGrades的值,来定义包含所需元素个数的grades数组:
int grades[nGrades];                         // Array storing nGrades values


显然,数组的长度值必须在这个语句之前定义。.

五、完整程序

本文的完整程序如下所示

5.1 Main.h 文件程序

#ifndef MAIN_H
#define MAIN_H
#include <stdio.h>
#include <stdlib.h>
#endif

5.2 Main.c 文件程序


#define _CRT_SECURE_NO_WARNINGS
#include "Main.h"
int main()
{
  system("color 3E");
    size_t nGrades = 10;                    // Number of grades
    printf("Enter the number of grades: ");
    scanf("%zd", &nGrades);
    int grades[nGrades];                         // Array storing nGrades values
    long sum = 0L;                               // Sum of the numbers
    float average = 0.0f;                        // Average of the numbers
    printf("\nEnter the %u grades:\n", nGrades); // Prompt for the input
    // Read the ten numbers to be averaged
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("%2zd> ", i + 1);
        scanf("%d", &grades[i]);                   // Read a grade
        sum += grades[i];                          // Add it to sum
    }
    printf("The grades you entered are:\n");
    for (size_t i = 0; i < nGrades; ++i)
    {
        printf("Grade[%2zd] = %3d  ", i + 1, grades[i]);
        if ((i + 1) % 5 == 0)                         // After 5 values
            printf("\n");                            // Go to a new line                    
    }
    average = (float)sum / nGrades;                // Calculate the average
    printf("\nAverage of the %zd grades entered is: %.2f\n", nGrades, average);
  system("pause");
  return 0;
}


六、总结

6067a0eb9a854f188d2078010ce75564.jpg


本文主要介绍了C语言高级编程的数组的变长数组。

  • 介绍了一维数组的变长方式。
  • 介绍了二维数组的变长方式。

1a17dadfa02744ca9a611017c4d04b64.jpg

本文到这里就结束啦。

  • 希望本文的C语言数组的变长数组。
  • 能对你有所帮助。
相关文章
|
2天前
|
存储 编译器 C语言
C语言之数组
C语言之数组
24 0
|
2天前
|
C语言
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
|
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语言:内存函数(memcpy memmove memset memcmp使用)
C语言:内存函数(memcpy memmove memset memcmp使用)
|
20小时前
|
C语言
C语言—内存函数的实现和模拟实现(内存函数的丝绸之路)
C语言—内存函数的实现和模拟实现(内存函数的丝绸之路)
16 0
|
20小时前
|
C语言
C语言—字符函数与字符串函数(字符问题变简单的关键之技)
C语言—字符函数与字符串函数(字符问题变简单的关键之技)
3 0