The prototype of variable-length array (VLA), the declaration position, and the essence of variable-length array, vla prototype

Source: Internet
Author: User
Tags array definition

The prototype of variable-length array (VLA), the declaration position, and the essence of variable-length array, vla prototype

Variable-length array is a newly added content in the C99 standard of C language. The so-called "variable" does not mean that the size of the array of this type can be modified after it is created, the size of the variable-length array cannot be modified after it is created. So why is it "variable length"? It is because the dimension of the Variable Length array can be specified by variables. This feature makes up for the deficiency that variable dimensions before C99 can only be specified by constants or constant expressions.

There are some limitations on variable-length arrays. variable-length arrays can only be declared inside the function or as function parameters. And cannot be initialized in the Declaration.

Let's look at an example:

Write a program, initialize a 3x5 double array, and copy the array to another two-dimensional array using a function based on the variable length array. You also need to write a function based on a variable-length array to display the content of the two arrays. These two functions should be able to process any NxM array.

1 #include <stdio.h>
 2 
 3 void copy (int row, int col, double arr [] [col], double tar [row] [col]);
 4 void show (int, int, double arr [*] [*]);
 5
 6 int main (void) {
 7
 8 double num [3] [5] = {
 9 {2.3,1.2,5.2,6.1,1.1},
10 {4.2,3.2,6.2,1.7,7.4},
11 {8.5,6.2,4.7,2.8,2.1}
12};
13
14 double rain [5] [12] = {
15 {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
16 {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
17 {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
18 {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
19 {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
20};
twenty one     
22 double tar [3] [5] = {0};
23 double tar2 [5] [12] = {0};
twenty four     
25 copy (3,5, num, tar);
26 show (3,5, tar);
27
28 copy (5,12, rain, tar2);
29 show (5,12, tar2);
30
31 return 0;
32}
33
34 void copy (int row, int col, double arr [] [col], double tar [row] [col]) {
35 int i, j;
36 for (i = 0; i <row; i ++) {
37 for (j = 0; j <col; j ++) {
38 tar [i] [j] = arr [i] [j];
39}
40}
41}
42
43 void show (int row, int col, double arr [row] [col]) {
44 int i, j;
45 for (i = 0; i <row; i ++) {
46 for (j = 0; j <col; j ++) {
47 printf ("%. 1f", arr [i] [j]);
48 if (j! = Col-1) printf (",");
49}
50 printf ("\ n");
51}
52 printf ("-------------------------------------- \ n \ n");
53}
In this example, given two two-dimensional arrays, a 3X5 array and a 5X12 array, and in the example, the two arrays use the same function to process the copy or use the same function to display.

Which function is used for display:

void show (int row, int col, double arr [row] [col])
Is a variable-length array definition method, declare the integer representing the dimension before using the variable-length array of the dimension. Because variable names can be omitted in the prototype, the prototype can be written as:

void show (int, int, double arr [*] [*])
 

 

To be continued ......


Related Article

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.