Variable-length array of C

Source: Internet
Author: User

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

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.