The variable-length array is a new addition to the C99 standard, and its addition greatly facilitates our programming, so-called variable-length arrays, which are not variable-length arrays, but are allowed to use variables to define arrays. This allows us to write more versatile functions. The following is an example of the function sum2d that adds and returns all the values in a two-bit array.
#include <stdio.h>#defineSIZE 10#defineLOC 2#defineROW 4intSum2d (intLocintRowintNum[loc][row]); intMainvoid){ intresult2d; intNum2d[loc][row] = {{ A, -, to, the}, { A, +, the, the}}; RESULT2D=sum2d (LOC, ROW, num2d); printf ("The sum of the array sum2d is%d.\n", RESULT2D); return 0; } intSum2d (intLocintRowintNum[loc][row]) { intAnswer =0; intI, J; for(i =0; i < Loc; i + +) for(j =0; J < Row; J + +) Answer+=Num[i][j]; returnanswer; }
In the declaration section of the function, we use a flat-length array, which allows the program to handle two-dimensional arrays with different row and column numbers. But it is important to note that the order of the three parameters must be a row, a column before, and then an array variable . That must be:
int sum2d (intint int num[loc][row]);
and cannot be:
int sum2d (intint int, row);
Although this may be compiled through, it is definitely not recommended to use this approach. Of course, in the Declaration section, loc,row,num and other formal parameter names can be omitted. But at this point [] to add ' * ', that can be abbreviated as:
int sum2d (intint int [*][*]);
It is wrong to not add ' * '.
Variable-length array of C