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\n");
printf("Sum of all elements = %d\n", sum2d(ROWS, COLS, junk));
printf("2x6 array\n");
printf("Sum of all elements = %d\n", sum2d(ROWS - 1, COLS + 2, morejunk));
printf("3x10 VLA!\n");
printf("Sum of all elements = %d\n", 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
C lang: VLA(variable-length array)的更多相关文章
- FireDAC 出现Variable length column[*] overflow. Value length - [80], column maximum length
FireDAC 出现Variable length column[*] overflow. Value length - [80], column maximum length FireDAC的 TF ...
- spark模型error java.lang.IllegalArgumentException: Row length is 0
failure: Lost task 18.3 in stage 17.0 (TID 59784,XXXXX, executor 19): java.lang.IllegalArgumentExcep ...
- 转:zero length array问题
单看这文章的标题,你可能会觉得好像没什么意思.你先别下这个结论,相信这篇文章会对你理解C语言有帮助.这篇文章产生的背景是在微博上,看到@Laruence同学出了一个关于C语言的题,微博链接.微博截图如 ...
- 0-3为变长序列建模modeling variable length sequences
在本节中,我们会讨论序列的长度是变化的,也是一个变量 we would like the length of sequence,n,to alse be a random variable 一个简单的 ...
- variable 'QJsonArray array' has initializer but incomplete type
variable "xxx" has initializer but incomplete type 编译报以上错误 分析:“xxx”对应的类型没有找到,没包含定义该变量类型的头文 ...
- C lang:Pointer and multidimensional array
Xx_Introduction Double indrection:Address of Address;Pointer of Pointer Ax_Code #include<stdio.h& ...
- expand gcc case variable length
daniel@daniel-mint ~/vex $ bash gen.sh 0x10 0x1F case 10: case 11: case 12: case 13: case 14: case 1 ...
- C语言关键字之sizeof
C语言关键字 sizeof 是一个操作符,返回对象或类型所占内存字节数,类型为size_t(定义在<stddef.h>),有2种用法: sizeof unary-expression si ...
- Do you master on array in C ?
Do you master on array in C ? 因为新标准C99的支持变长数组, 差点儿C的标准特性就是看着gcc来的(Linux 内核严重依赖GCC) int mani() { cons ...
随机推荐
- Android 中的Activity、Window、View之间的关系
一.概述 Activity 可以说是应用程序的载体(也可以理解为界面的载体,但是不界面),用户能够在上面绘制界面(Activity本身不绘制界面),并提供用户处理事件的API,维护应用程序的生命周 ...
- Java修炼——反射机制
反射机制常见的作用: 1) 动态的加载类.动态的获取类的信息(属性,方法,构造器) 2) 动态构造对象 3) 动态调用类和对象的任意方法.构造器 4) 动态调用和处理属性 5) 获取泛型信息 6) 处 ...
- ZOJ 2112 Dynamic Rankings(树状数组+主席树)
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...
- CF 1136A 1136B 1136C 1136D 1136E(Round546ABCDE)题解
题目地址:https://codeforces.com/contest/1136 A: Nastya Is Reading a Book 题解:挨个判断即可,水题. 参考代码: #include< ...
- ACM中java中BigInteger和Decimal用到的主要函数
java中大数以及高精度常用函数 使用java大数类解决问题时我们需要注意两个方面:1.不能有包名,也就是说我们要把主类放到默认的包里,如果你的代码里出现形如package cn.gov.test;这 ...
- 洛谷 题解 P1736 【创意吃鱼法】
题目大意 给出一个 \(n \times m \ (1 \leq n, \ m \leq 2500)\) 的 \(01\) 矩阵,让你在其中找到一个最大的子矩阵使得该子矩阵除了一条对角线上的数字均为 ...
- python做单因素方差分析
方差分析的主要功能就是验证两组样本,或者两组以上的样本均值是否有显著性差异,即均值是否一样. 这里有两个大点需要注意:①方差分析的原假设是:样本不存在显著性差异(即,均值完全相等):②两样本数据无交互 ...
- 深入探索Java设计模式(二)之策略模式
策略设计模式是Java API库中常见的模式之一.这与另一个设计模式(称为状态设计模式)非常相似.本文是在学习完优锐课JAVA架构VIP课程—[框架源码专题]中<学习源码中的优秀设计模式> ...
- linux-linux mysql5.7主从搭建
原理: 1:master,建立二进制日志 , 每产生语句或者磁盘变换,写进日志: 2:slave,建立relaylog日志(中继日志),分析master的binlog. 3:master,建立授权账号 ...
- 大数据项目2(Java8聚合操作)
前言:为很好的理解这些方法,你需要熟悉java8特性Lambda和方法引用的使用 一:简介 我们用集合的目的,往往不是简单的仅仅把数据保存哪里.而是要检索(遍历)或者去计算或统计....操作集合里面的 ...