C + + Tutorial Chapter III-Arrays

Source: Internet
Author: User
Tags first string

Reprint please specify the source:

Http://blog.csdn.net/miaoyunzexiaobao

PS: The concept of namespaces: previously written std::cout/std::cin/std::endl, etc., means that cout/cin/endl these belong to a namespace named Std, and would not write the words can be declared before main: using namespace STD, which indicates that this function uses the namespace Std. The concept of namespaces will be explained later.

1. The concept of arrays

An array is a collection of values of a set of types, a composite data type consisting of a type name, an identifier, and a number of dimensions. Where the type name specifies the type to which the array can be stored, and the number of dimensions that specify the number of elements contained in the array.

For example, define an integer array of length 10: int a[10];

2. Initialization of arrays

The subscript of the array starts with 0. That is, an array of length 3 int a[3], where the element is a[0],a[1],a[2].

After an array is defined, if it is not initialized, the array is initialized based on the default initialization rules of its type to the elements within the group. Examples are as follows:

Code:

#include <iostream> using namespace std; int A_global[10];bool b_global[10]; int _tmain (int argc,_tchar* argv[]) {    int a[10];    BOOL B[10];    cout<< "a[10]:";    for (int i = 0; i < ++i)        cout<<a[i]<< "";    cout<<endl<< "a_global[10]:";    for (int i = 0; i < ++i)        cout<<a_global[i]<< "";    cout<<endl<< "b[10]:";    for (int i = 0; i < ++i)        cout<<b[i]<< "";    cout<<endl<< "b_global[10]:";    for (int i = 0; i < ++i)        cout<<b_global[i]<< "";     return 0;}


, the elements defined in A_global and b_global outside of the function are initialized to 0, while the values defined in the array within the function are undefined.

When the first value of an array is initialized, other elements in the array are initialized to 0 if the other elements of the arrays are not initialized:

int _tmain (int argc,_tchar* argv[]) {    int a[10] ={3};    BOOL b[10] ={4};    cout<< "a[10]:";    for (int i = 0; i < ++i)        cout<<a[i]<< "";    cout<<endl<< "b[10]:";    for (int i = 0; i < ++i)        cout<<b[i]<< "";    return 0;}


Note that B is an array of type bool, and when you assign 4 to b[0], b[0] becomes true. When output B[0], the bool turns int,true to 1, that is, the output 1


Displays the initialization array: The element is set when the array is defined. When the display is initialized, you can specify no array dimensions. For example: Int a[]={1,2,3}, which defines an array of length 3, and assigns three of them to three elements.

In addition, arrays do not allow direct assignment and replication. That is, you cannot copy an array to another array, or assign a value to another array.

3. Multidimensional arrays

Previously said is a one-dimensional array, now look at two-dimensional: int a[3][3];

A two-dimensional array is similar to a one-dimensional array, and one example is clear:

int _tmain (int argc,_tchar* argv[]) {    int a[3][2]= {{1,2},{3,4},{5,6}};     for (int i = 0, i < 3; ++i) for        (int j = 0; j < 2; ++j)            cout<<a[i][j]<< "";    cout<<endl;     return 0;}


The number of the first dimension in a multidimensional array can be omitted, and the system can calculate the size of the first dimension based on the number of elements used to initialize the array: int a[][2] = {{1,2},{3,4},{5,6}}; The system will set the size of the first dimension to 3.

In fact, we can see that the two-dimensional array is composed of multiple one-dimensional arrays. In fact, there is no concept of multidimensional array in the computer, and the multidimensional array set in the program is automatically converted into one-dimensional array at the bottom of the computer.

4. Array name

It is now known that when an array is used, it can be called by the array name + subscript. But what happens when you directly output the array name?


You can see that when printing a and a+1 directly, the system prints two hexadecimal digits. In fact, when defining an array, the system opens up a space in memory for the array. Where the array name represents the address of the space. Therefore, when you print a, the address of the first number a[0] of the array a[10] is printed. The address of the second number a[1] is printed when the a+1 is printed.


Here is an introduction to the knowledge Point: The dereference "*". When you use a dereference for an address, it means getting the value that the address points to:


That


i.e. a[0] = = *a, a[1] = = * (a+1) ... a[9] = = * (a+9)

Try another two-dimensional array:


Here a still represents the array address. But what is a[0]? As mentioned before, a two-dimensional array is actually a combination of multiple one-dimensional arrays. The a[2][3] is actually made up of a one-dimensional array of two dimension 3. Therefore, a[0] should be the address of the first one-dimensional array, a[1] should be the address of the second one-dimensional array. So how do I get the first number in the first one-dimensional array? It is still a reference to the solution. So there are: a[0][0] = = **a, a[1][0] = * * (a+1). Similarly, a[0][1] = = * (* (a) +1), a[1][1] = = * (* (a+1) +1) ...

5. Array length

If the array subscript is out of bounds, the system generates an error. It is also not possible to initialize arrays beyond the number of array lengths. Like what:
int a[3]={1,2,3,4}; A compilation error is generated.

Here by the way, a string in C + + is mentioned. For example, "Hello", the string length is 6. After HELLO the system adds a hidden ' + ':


sizeof and the strlen Introduction: ( quoted from Baidu Encyclopedia )

sizeof is a C + + an operator in the operator it simply means that it returns the number of bytes of memory that an object or type occupies.

strlen is just a counter work that starts scanning from somewhere in the memory (which can be the beginning of a string, somewhere in the middle, or even an indeterminate area of memory) until it touches the first string Terminator ' + ' and then returns the counter value ( length does not include " /") .

Therefore, if you do the following:

Char a[5] = "HELLO" is also wrong. should be set to 6.

As for why there is a ' \ s ' behind the string because C + + is compatible with C. In the C language, a string is represented by an array of char* or char, and its operation on the string is agreed to read to '/' to terminate. such as the strlen,strcmp,strxxx in C language. Students who have studied C should be impressed.

The char* in C + + is no longer represented by a string, but instead is expressed in string. About string, which will be explained later.

C + + Tutorial Chapter III-Arrays

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.