C ++ Array

Source: Internet
Author: User

Declares an int object in the following format:
Int Ia [10];

1.

We must specify a dimension greater than or equal to 1 for the array, and the dimension value must be a constant expression -- that is: this means that non-const variables cannot be used to specify the dimension of the array. The following example contains valid and invalid array definitions.
Extern int get_size ();
// Both buf_size and max_files are const
Const int buf_size = 512, max_files = 20;
Int staff_size = 27;
 
// OK: const variable

Char input_buffer [buf_size];
 
// OK constant expression: 20-3
Char * filetable [max_files-3];
 
// Error: Non-const variable
Double salaries [staff_size];
 
// Error non-const expression
Int test_scores [get_size ()];

Note:

Although staff_size is initialized by a text constant, staff_size itself is a non-const object system that can only access its value at runtime. Therefore, it is invalid as an array dimension.

On the other hand, the expression max_files-3 is a constant expression. Because max_files uses 20 as the const variable of the initial value, this expression is calculated as 17 during compilation.

2.

Arrays can be explicitly initialized with a set of numbers separated by commas in braces. For example:
Const int array_size = 3;
Int Ia [array_size] = {0, 1, 2 };

The dimension value does not need to be specified for the explicitly initialized array. The Compiler determines the dimension of the array based on the number of elements listed.
// Array with a dimension of 3
Int Ia [] = {0, 1, 2 };
If the dimension is specified, the number of elements provided in the initialization list cannot exceed this value. Otherwise, a compilation error occurs. If the specified dimension is greater than the number of given elements, the elements not explicitly initialized will set to 0
// Ia ==>{ 0, 1, 2, 0, 0}
Const int array_size = 5;
Int Ia [array_size] = {0, 1, 2 };
Character Arrays can use a comma-separated character text list to initialize the text list and enclose it in curly brackets, or use a string text for initialization. Note that these two forms are not equivalent, A String constant contains an additional null ending character, such
Const char CA1 [] = {'C', '+', '+ '};
Const char Ca 2 [] = "C ++ ";

The dimension of Cal is 3. the dimension of CA is 4. The declaration below will be marked as an error.
// Error: "Daniel" is a 7-Element
Const char CH3 [6] = "Daniel ";

5.

An array cannot be initialized by another array or assigned to another array, and C ++ cannot declare a referenced array (an array composed of references)
 
Const int array_size = 3;
Int IX, JX, kx;
 
// OK: array of pointers of the int type *
Int * IAP [] ={& IX, & JX, & kx };
 
// Error: array cannot be referenced
Int & IAR [] = {IX, JX, kx };
 
// Error: An array cannot be initialized using another array.
Int ia2 [] = IA; // Error
Int main ()
{
Int ia3 [array_size]; // OK
// Error: One array cannot be assigned to another Array
Ia3 = IA;
Return 0;
}

4.

To copy an array to another, each element must be copied in order, for example
Const int array_size = 7;
Int ia1 [] = {0, 1, 2, 3, 4, 5, 6 };
 
Int main ()
{
Int ia2 [array_size];
For (int ix = 0; ix <array_size; ++ IX)
Ia2 [ix] = ia1 [ix];
 
Return 0;
}

5.

Any expression whose result is an integer can be used to index the array, for example
Int someval, get_index ();
Ia2 [get_index ()] = someval;

However, the user must be clear that C ++ does not provide the Compilation Time or runtime time to check the range of objects under the array, except that the programmer pays attention to the details and thoroughly tests his/her ownProgramIn addition, there is no other way to prevent the array from being out-of-bounds and there are still fatal errors in the program that can be compiled and executed. This is not impossible.

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.