[C + +] Array

Source: Internet
Author: User

Tag: Type causes character array undefined span reference function ptr particularity

Array

Arrays are containers that hold objects of the same type. These objects themselves have no name and need to be accessed through their location. The size of the array is determined to be constant, and you cannot add elements to the array arbitrarily.

An array is a composite type that is declared as a[d],a an array name, and B is an array dimension (which describes the number of elements in a set).

Array initialization

By default, the elements of an array are initialized by default. An array of a built-in type is defined inside the function, so the default initialization order array contains undefined values. An array of built-in types is defined outside the function body, so the default initialization order array contains a value of 0.

Explicitly initializing an array element

Constunsigned sz =3;intIa1[sz] = {0,1,2};//sz is constantintA2[] = {0,1,2};//the array latitude can be empty. Determines the array dimension during compilation based on the number of array initial valuesinta3[5] = {0,1,2};//equivalent to a3[] = {0, 1, 2, 0, 0};stringa4[3] = {"Hi","Bye"};//equivalent to a4[] = {"Hi", "Bye", ""};inta5[2] = {0,1,2};//error, the initial value exceeds the dimension of the array
The particularity of character array

When initializing character arrays with string literals, it is important to note that there is also a null character at the end of the string literal, which is copied to the character array as the other ego of the string.

CharA1[] = {'C','+','+'};//list initialization, no null charactersCharA2[] = {'C','+','+',' /'};//explicit NULL charactersCharA3[] ="C + +";//automatically adds a null character that represents the end of a stringConst Chara4[6] ="Daniel";//error. Because the string literal has a null character at the end, it causes the array space to be insufficient
Complex array declarations (difficulty)
int *ptrs[]; // Ptrs is an array that contains 10 integer pointers int (*parray) [ten] = &arr; // Parray points to an array containing 10 integers int (&ARRREF) [ten] = arr; // Arrref A reference to an array of 10 integers int * (&arry) [ten] = ptrs; // Arry is a reference to an array that contains 10 pointers
Pointers and Arrays

[C + +] Array

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.