zoukankan      html  css  js  c++  java
  • 变长数组(variable-length array,VLA)(C99)

    处理二维数组的函数有一处可能不太容易理解,数组的行可以在函数调用的时候传递,但是数组的列却只能被预置在函数内部。例如下面这样的定义:

     1 #define COLS 4
     2 int sum3d(int ar[][COLS], int rows)
     3 {
     4     int r, c, tot;
     5     tot = 0;
     6 
     7     for(r = 0; r < rows; r++)
     8         for(c = 0; c < COLS; c++)
     9             tot += ar[r][c];
    10     return tot;
    11 }

    现在假定了如下的数组:

    1 int array1[5][4];
    2 int array2[100][4];
    3 int array3[2][4];

    可以使用下面的函数调用:

    1 tot = sum2d(array1, 5);
    2 tot = sum2d(array2, 100);
    3 tot = sum2d(array3, 2);

    这是因为行数可以传递给参量rows,而rows是一个变量。但是如果要处理6行5列的数组,则需要创建另一个函数。
    创建一个处理任意的二维数组也是有可能的,但是比较繁琐。

    正是以上原因,C99标准引入了变长数组,它允许使用变量定义数组各维,e.g.你可以使用下面的声明:

    int quarters = 4;
    int regions = 5;
    double sales[quarters][regions]; //一个变长数组VAL

    变长数组有一些限制:变长数组必须是自动存储类的,意味着它们必须在函数内部或作为函数参数声明,而且声明时不可以进行初始化。
    先看一个简单的例子,简单的阐明如何编写一个计算任意二维int数组的和的函数

    1 int sum2d(int rows, int cols, int ar[rows][cols]);
    2 //rows, cols 要先于ar[][]

    C99标准规定,可以省略函数原型中的名称,但是如果省略名称,则需要用星号来代替省略的维数:

    int sum2d(int , int, int ar[*][*]);
     1 #include<stdio.h>
     2 
     3 #define ROWS 3
     4 #define COLS 4
     5 
     6 int sum2d(int rows, int cols, int ar[rows][cols])
     7 {
     8     int i, j, sum;
     9     sum = 0;
    10 
    11     for(i = 0; i < rows; i++)
    12         for(j = 0; j < cols; j++)
    13             sum += ar[i][j];
    14     return sum;
    15 }
    16 
    17 int main()
    18 {
    19     int i, j;
    20     int junk[ROWS][COLS] = {
    21         {2, 4, 6, 8},
    22         {3, 5, 7, 9},
    23         {12, 10, 8, 6}
    24     };
    25 
    26     int morejunk[ROWS-1][COLS+2] = {
    27         {20, 30, 40, 50, 60, 70},
    28         {5, 6, 7, 8, 9, 10}
    29     };
    30 
    31     int varr[rs][cs]; //变长数组
    32 
    33     for(i = 0; i < rs; i++)
    34         for(j = 0; j < cs; j++)
    35             varr[i][j] = i * j +j;
    36     printf("3 * 5 array
    ");
    37     printf("sum of all elemts = %d
    ",sum2d(ROWS, COLS, junk));
    38 
    39     printf("2 * 6 array
    ");
    40     printf("sum of all elemts = %d
    ",sum2d(ROWS - 1, COLS + 2, morejunk));
    41 
    42     printf("3 * 10 array
    ");
    43     printf("sum of all elemts = %d
    ",sum2d(rs, cs, varr));
    44 
    45     return 0;
    46 }
    47 
    48 C代码
  • 相关阅读:
    最新的Delphi版本号对照
    SuperObject生成示例
    Why does Delphi XE7 IDE hangs and fails on out of memory exception?
    使用 TRESTClient 与 TRESTRequest 作为 HTTP Client(转)
    Delphi提取PDF文本
    (3)PyCharm中Flask工程逆向生成数据库表
    (2)PyCharm开发Flash项目之蓝图构建
    (1)PyCharm开发工具安装Flask并创建helloworld程序
    使用localStorage写一个简单的备忘录
    一个Redis实例适合存储不同应用程序的数据吗?
  • 原文地址:https://www.cnblogs.com/wuruofeng/p/8326137.html
Copyright © 2011-2022 走看看