zoukankan      html  css  js  c++  java
  • C lang: VLA(variable-length array)

    Xx_VLA Introduction

    • VLA:variable-length array,not variable array size,but variable arary dimensionality size.
    • Must be an automatic storage type
    • They cannot be initialized in a declaration
    • VLA is new feature,depend on compiler support.

    Ax_Code

    #include<stdio.h>
    #define ROWS 3
    #define COLS 4
    int sum2d(int rows, int cols, int ar[rows][cols]);
    
    int main(void)
    {
        int i, j;
        int rs = 3;
        int cs = 10;
        int junk[ROWS][COLS] =
        {
            2, 4, 6, 8,
            3, 5, 7, 9,
            12, 10, 8, 6
        };
    
        int morejunk[ROWS - 1][COLS + 2] =
        {
            20, 30, 40, 50, 60, 70,
            5, 6, 7, 8, 9, 10
        };
    
        int vari[rs][cs];       // VLA
    
        for ( i = 0; i < rs; i++)
            for (j = 0; j < cs; j++)
                vari[i][j] = i * j + j;
        printf("3x5 array
    ");
        printf("Sum of all elements = %d
    ", sum2d(ROWS, COLS, junk));
        printf("2x6 array
    ");
        printf("Sum of all elements = %d
    ", sum2d(ROWS - 1, COLS + 2, morejunk));
        printf("3x10 VLA!
    ");
        printf("Sum of all elements = %d
    ", sum2d(rs, cs, vari));
    
        return 0;
    }
    
    int sum2d(int rows, int cols, int ar[rows][cols])
    {
        int r;
        int c;
        int tot;
        tot = 0;
    
        for (r = 0; r < rows ; r++)
            for (c = 0; c < cols; c ++)
                tot = tot + ar[r][c];
    
        return tot;
    }
    

    3x5 array
    Sum of all elements = 80
    2x6 array
    Sum of all elements = 315
    3x10 VLA!
    Sum of all elements = 270
    

    END

  • 相关阅读:
    linux内核模块的程序结构
    Vmware中RedHat命令行和图形界面切换
    Linux2.6内核启动流程学习
    关于mini2440u_boot的制作及烧录
    linux指令
    Arm-linux-gcc-4.3.2安装步骤
    JNDI 与 LDAP
    apicloud 自定义模块引用aar
    CSS canvas 捕捉视频video元素截图
    本地服务器搭建服务:ftp
  • 原文地址:https://www.cnblogs.com/enomothem/p/11930176.html
Copyright © 2011-2022 走看看