Variable-length array (variable-length ARRAY,VLA)

Source: Internet
Author: User

A function that handles a two-dimensional array may not be easy to understand, and the rows of the array can be passed at the time of the function call, but the columns of the array can only be preset inside the function. For example, the following definition:

1 #defineCOLS 42 intSum3d (intAr[][cols],introws)3 {4     intR, c, tot;5tot =0;6 7      for(R =0; R < rows; r++)8          for(c =0; c < COLS; C++)9Tot + =Ar[r][c];Ten     returntot; One}

The following array is now assumed:

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

You can use the following function call:

1 5 ); 2  - ); 3 2);

This is because the number of rows can be passed to the parameter rows, and rows is a variable. However, if you are working with an array of 6 rows and 5 columns, you need to create another function.
It is also possible to create an arbitrary two-dimensional array, but it is cumbersome.

For these reasons, theC99 standard introduces variable-length arrays, which allow variables to be used to define the dimensions of an array , e.g. you can use the following declaration:

int 4 ; int 5 ; Double // A variable-length array val

Variable-length arrays have some limitations: variable-length arrays must be auto-stored classes, meaning they must be declared inside the function or as function arguments, and cannot be initialized at the time of declaration.
Let's start with a simple example of how to write a function that computes the same as an array of any two-dimensional int.

1 int sum2d (intint int ar[rows][cols]); 2 // rows, cols before ar[][]

The C99 standard specifies that names in function prototypes can be omitted, but if names are omitted, asterisks are used in place of the omitted dimensions:

int sum2d (intint int ar[*][*]);
1#include <stdio.h>2 3 #defineROWS 34 #defineCOLS 45 6 intSum2d (intRowsintColsintAr[rows][cols])7 {8     intI, J, sum;9sum =0;Ten  One      for(i =0; i < rows; i++) A          for(j =0; J < cols; J + +) -Sum + =Ar[i][j]; -     returnsum; the } -  - intMain () - { +     intI, J; -     intJunk[rows][cols] = { +{2,4,6,8}, A{3,5,7,9}, at{ A,Ten,8,6} -     }; -  -     intmorejunk[rows-1][cols+2] = { -{ -, -, +, -, -, -}, -{5,6,7,8,9,Ten} in     }; -  to     intVARR[RS][CS];//variable-length arrays +  -      for(i =0; I < RS; i++) the          for(j =0; J < CS; J + +) *VARR[I][J] = i * j +J; $printf"3 * 5 array\n");Panax Notoginsengprintf"sum of all Elemts =%d\n", sum2d (ROWS, COLS, Junk)); -  theprintf"2 * 6 array\n"); +printf"sum of all Elemts =%d\n", sum2d (ROWS-1, COLS +2, Morejunk)); A  theprintf"3 * array\n"); +printf"sum of all Elemts =%d\n", sum2d (RS, CS, Varr)); -  $     return 0; $ } -  -C code

Variable-length array (variable-length ARRAY,VLA)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.