Definition and initialization of static arrays in C Language

Source: Internet
Author: User
Tags array definition
Array can be initialized, that is, when it is defined, it contains the values that can be used immediately by the program.
For example, the following code defines a Global Array and initializes it with a set of Fibonacci numbers:
Int iarray [10] = {,); // Initialization
Void main ()
{
//...
}
The number of initialized array values cannot be more than the number of array elements. The value of initialized array cannot be omitted by skipping commas. This is allowed in C, however, it is not allowed in C ++.
For example, the following code initializes the array incorrectly:
Int arrayl [5] = {1, 2, 3, 4, 5, 6}; // error: the number of initialization values is greater than the number of array elements.
Int array2 [5] = {1, 2, 3, 4}; // error: the initialization value cannot be omitted.
Int array3 [5] = {1, 2, 3,}; // error: the initialization value cannot be omitted.
Int array4 [5] ={}; // error: syntax format Error
Void main ()
{
//...
}
The number of initialization values can be less than the number of array elements. When the number of initialization values is less than the number of array elements, the corresponding values are initialized in order. The subsequent Initialization is 0 (Global or static array) or an uncertain value (partial array ).
For example, the following program initializes the array:
//*********************
// ** Ch7_2.cpp **
//*********************

# Include <iostream. h>

Int array1 [5] = {1, 2, 3 };
Static int array2 [5] = {1 };

Void main ()
{
Int arr1 [5] = {2 };
Static int arr2 [5] = {1, 2 };

Int N;
Cout <"Global:/N ";
For (n = 0; n <5; n ++)
Cout <"" <array1 [N];

Cout <"/nglobal static:/N ";
For (n = 0; n <5; n ++)
Cout <"" <array2 [N];

Cout <"/nlocal:/N ";
For (n = 0; n <5; n ++)
Cout <"" <arr1 [N];

Cout <"/nlocal static:/N ";
For (n = 0; n <5; n ++)
Cout <"" <arr2 [N];
Cout <Endl;
}

The running result is:
Global:
L 2 3 0 0
Global static:

1 0 0 0 0
Local:
2 23567 23567 23567 23567
Local static:
1 2 0 0 0
In this example, the initialization of global arrays and Global static arrays is completed before the main function is run, and the initialization of local arrays and local static arrays is completed after the main function is entered.
The value of the Global Array arrayl [5] is initialized to 1, 2, 3 in order for the value of the initialization table, and the value of two elements is initialized to 0 by default.
The initialization of the Global static array array2 [5] is the same as that of the global array. The initialization Table value (1) indicates the value of the 1st elements, rather than that all the array elements are 1.
The local array arrl [5] is initialized in order based on the content of the initialized Table value. Because there are only one initialized Table value, the values of the four elements are uncertain. Here the value is 23567.
The local static array arr2 [5] is initialized in sequence based on the initialization table, and the values of the remaining three array elements are initialized to 0 by default.

2. initialize the character array

There are two methods to initialize the character array:
Char array [10] = {"hello "};
The other is:
Char array [10] = {'h', 'E', 'l', 'l', '/0 '};
The first method is widely used. during initialization, the system automatically uses the array where no value is filled. In addition, curly braces in this method can be omitted, which can be expressed:
Char array [10] = "hello ";
The second method initializes an array with an element at a time, just as initializing an integer array. This method is usually used to input invisible characters that are not easily generated on the keyboard.
For example, the initialization value in the following code is several tabs:
Char charray [5] = {'/t','/0 ');
Do not forget to allocate space for the last, '/0. If you want to initialize a string "hello", the array defined for it has at least six array elements.
For example, the following code initializes the array, but may cause unexpected errors:
Char array [5] = "hello ";
This code will not cause compilation errors, but it is dangerous to rewrite memory units other than the array space.

3. omitted array size

An array definition with initialization can omit the array size in square brackets.
For example, the following code defines an array as five elements:
Int A [] = {2, 4, 6, 8, 10 };
  The size of the array must be known during compilation.Usually, the number in square brackets determines the size of the array. If an array definition is initialized and the array size in square brackets is omitted, the compiler calculates the number of elements between brackets to obtain the array size.
For example, the following code produces the same result:
Static int A1 [5] = {1, 2, 3, 4, 5 };
Static int A2 [] = {1, 2, 3, 4, 5 };
There are several advantages for the compiler to get the size of the initialization array. It is often used to initialize an array determined by the number of elements in the initialization process, providing programmers with the opportunity to modify the number of elements.
How can I know the size of an array without specifying the array size? The sizeof operation solves this problem. For example, the following code uses sizeof to determine the size of the array:

//*********************
// ** Ch7_3.cpp **
//*********************

# Include <iostream. h>

Void main ()
{
Static int A [] = {1, 2, 4, 8, 16 };
For (INT I = 0; I <(sizeof (a)/sizeof (INT); I ++)
Cout <A [I] <"";
Cout <Endl;
}

The running result is:
1 2 4 8 16
The sizeof operation makes the for loop automatically adjust the number of times. If you want to add or delete elements from the set where array a is initialized, you only need to re-compile the elements. Other content does not need to be changed.
The storage volume occupied by each array can be determined using the sizeof operation! Sizeof returns the number of bytes of the specified item. Sizeof is commonly used in arrays, so that code can be transplanted between 16-bit machines and 32-bit machines:
For string initialization, note that the size of the space actually allocated by the array is the number of characters in the string plus the ending character, '/0.
For example, the following code defines an array of characters:

//*********************
// ** Ch7_4.cpp **
//*********************

# Include <iostream. h>

Void main ()
{
Char ch [] = "how are you ";

Cout <"size of array:" <sizeof (CH) <Endl;
Cout <"size of string:" <strlen ("how are you") <Endl;
}

The running result is:
Size of array: 12
Size of string: LL
In this example, the array size is 12, and the string length is 11.
  The omitted array size can only be defined in the initialized array.
For example, the following code produces a compilation error:
Int A []; // error: the array size is not determined.
  When defining an array, the compiler must know the size of the array in any case.

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.