C + + Array manipulation

Source: Internet
Author: User
Tags array definition

First, array definition and initialization

   1: one-dimensional array initialization:2: Standard Way One:intvalue[ -];//Value[i] With variable value, no initialization   3: Standard way Two:intvalue[ -] = {1,2};//value[0] and value[1] values of 1 and 2, respectively, and no defined value[i>1]   4://is initialized to 0   5: pointer mode:int* Value =New int[n];//not initialized   6:Delete[]value;//must not forget to delete the array space   7:     8: Two-dimensional array initialization:9: Standard Way One:intvalue[9][9];//value[i][j] With variable value, no initialization  Ten: Standard way Two:intvalue[9][9] = {{1,1},{2}};//value[0][0,1] and value[1][0] values are initialized, others are initialized to 0   One: pointer mode one:int(*value) [N] =New int[M][n];  A:Delete[]value;//n must be a constant, call intuitive. Not initialized   -: pointer mode two:int* * value =New int*[M];  -: for(i) value[i] =New int[n];  the: for(i)Delete[]value[i];  -:Delete[]value;//multiple destruction, storage hassle, uninitialized   -: pointer mode three:int* Value =New int[3][4];//The storage of an array is stored by row   -:Delete[]value;//Be sure to release the memory, otherwise it will cause a memory leak   +:     -: Multidimensional array initialization: +: pointer mode:int* Value =New int[M] [3][4];//only the first dimension can be a variable, and the other dimensions must be constants, or they will error   A:Delete[]value;//Be sure to release the memory, otherwise it will cause a memory leak

Add ";" After the curly brace of the array initialization. to indicate the end.

Array Access:

Pointer form: Accessed as a two-dimensional array value[i][j]:

* (Value[i] + j) or

(* (value + i)) [j]

Second, the array is passed as a parameter

   1: one-dimensional array parameter passing:2:voidFunc (int*value); 3: Or is4:voidFunc (intvalue[]); 5:     6: Two-dimensional array delivery:7: Definition isint**the delivery of value;8:voidFunc (int**value); 9: Definition isint(*value) [N] =New int[m][n]; the transferTen:voidFuncint(*value) [n]);//sizeof (P) =4,sizeof (*value) =sizeof (int) *n;

Three, array and pointer relationship

1, the connotation of the array name is that the reference entity is a data structure, this data structure is an array;

2, the extension of the array name is that it can be converted to a pointer to its reference entity, but also a pointer constant;

3, pointer to the array is another type of variable, (under the Win32 platform, the length of 4), only means that the group storage address.

4, the 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.

Iv. storage format for arrays

Multidimensional arrays are stored in memory in the lowest-dimensional contiguous format, such as the two-dimensional array {{1,2},{3,4}} in memory where the "1,3,2,4" is the order, which is different from Matlab,matlab is stored by column . Useful when using pointers for indexing.

Five, character array

An array of type char is called a character array, which is typically used to store strings. A string is a sequence of characters appended with a special character (a trailing flag). The string termination character indicates that the character has ended, defined by the escape sequence '% ', which is sometimes referred to as a null character and occupies one byte, where 8 bits are all 0. This form of string is often referred to as a type C string, because defining a string in such a way is introduced in the C language, using string in C + +, and the CString class is defined in MFC.

Each character in a string occupies one byte, and the last null character is counted, and the string requires more bytes than the number of bytes contained. Such as:

Char movie_star[15] = "Marilyn Monroe";

Here the string is 14 characters, but to define an array of 15 strings. You can also not specify the number of character arrays. Such as:

Char movie_star[] = "Marilyn Monroe";

VI. Memory leaks

We define a pointer, give it an address value, and then no longer use it, but without delete, the original memory will not be freed when the pointer is given a different address value, which is called a memory leak.

Category: C + + Learning

C + + Array manipulation

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.