还可以在执行期间确定二维或多维数组中的任意维或所有维。
例如:
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
![img](https://img-blog.csdnimg.cn/img_convert/4e43854e64bca6c411c5fc07dcf695b3.png)
![img](https://img-blog.csdnimg.cn/img_convert/287beff485abfb270f8fa78813b4f5c9.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**