C + + arrays

Source: Internet
Author: User

Tag: A string byte function memory release must be dynamically assigned to transfer otherwise

One or one-D arrays

      1. static int array[100]; An array of arrays is defined, and is not initialized with a group
      2. static int array[100] = {n}; The array of arrays is defined and initialized
      3. Dynamic int* array = new INT[100];  delete []array; Array with a length of 100 assigned
      4. Dynamic int* array = new int[100] (UP); delete []array; Initializes the first two elements for an array of length 100

Two or two-D arrays

      1. static int array[10][10]; An array is defined and is not initialized
      2. static int array[10][10] = {{n}, {2,2}}; Array initialized array[0][0,1] and array[1][0,1]
      3. dynamic int (*array) [n] = new Int[m][n]; delete []array;
      4. Dynamic int** array = new INT*[M];  for (i) array[i] = new Int[n]; for (i) delete []array[i];    delete []array; Multiple destruction
      5. Dynamic int* array = new Int[m][n];      delete []array; Arrays are stored by row

Three, multidimensional arrays

int* array = new INT[M][3][4]; Only the first dimension can be a variable, the other dimension must be a constant, or it will be an error

delete []array; Memory must be released, otherwise the memory will leak

Four, array as function parameter pass

      1. One-dimensional array delivery:
        1. void func (int* array);
        2. void func (int array[]);
      2. Two-dimensional array delivery:
        1. void func (int** array);
        2. void func (int (*array) [n]);

Array name as a function parameter, in the function body, it loses its own connotation, just a pointer, and in its loss of its connotation at the same time, it also lost its constant characteristics, can be self-increment, self-reduction and other operations, can be modified.

Five, character array

An array of type char is a character array of the Standing Committee, in which the last digit in the character array is the transfer character ' + ' (also a null character), which indicates that the string has ended. The string class is defined in C + + and the CString class is defined in Visual C + +.

Each character in a string occupies one byte, plus the last null character. Such as:

Char array[10] = "Cnblogs";

Although there are only 7 bytes, the string length is 8 bytes.

You can also not define string lengths, such as:

Char array[] = "Cnblogs";

C + + arrays

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.