Variable-length array in C Language

Source: Internet
Author: User
Variable-length array in C Language

We know that the traditional C language does not support variable-length arrays as C ++ does. That is to say, the length of arrays is determined during compilation and cannot be changed during runtime. The new features of C language are defined in the c99 standard. A new feature allows you to use variable-length arrays in C.

C99 gives C programmers the ability to use variable length arrays, which are arrays whose sizes are not known until run time. A Variable Length array declaration is like a fixed array declaration limit t that the array size is specified by a non-constant expression. when the Declaration is encountered, the size expression is evaluated and the array is created with the indicated length, which must be a positive integer. once created, variable length array cannot change in length. elements in the array can be accessed up to the allocated length; accessing elements beyond that length results in undefined behavior. there is no check required for such out-of-range accesses. the array is destroyed when the block containing the Declaration completes. each time the block is started, a new array is allocated.


The above is the c99 standard description of the C language variable-length array.

C Variable Length array, the source code used for testing is very simple, as shown below:

  1. // File name: dynarray. c
  2. // Compilation environment: bloodshed Dev-C/C ++ 4.9
  3. # Include <stdio. h>
  4. # Define bzero (B, Len) (memset (B), '/0', (LEN), (void) 0)

  5. Int main (INT argc, char * argv [])
  6. {
  7. Int I, N;

  8. N = atoi (argv [1]);
  9. Char arr [n + 1];
  10. Bzero (ARR, (n + 1) * sizeof (char ));

  11. For (I = 0; I <n; I ++ ){
  12. Arr [I] = (char) ('A' + I );
  13. }
  14. Arr [N] = '/0 ';
  15. Printf ("% s/n", arr );

  16. Getchar ();
  17. Return (0 );

  18. }

The above program is named dynarray. C. The job is to add 1 to the value n of argv [1] As the length of ARR, And the ARR type of the variable-length array is Char. Then write some characters into the array and output the written strings.

After compiling on the c 99 standard IDE, run:

C:/c99_test/dynarray.exe 6

Output:

Abcdef


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.