Summary of problems related to initialization of C string array
In C programming, when we declare a string array, we often need to initialize it to an empty string. The following three ways are summed up:
- (1) Char str[10]= "";
- (2) Char str[10]={' + '};
- (3) Char str[10]; str[0]= ' + ';
The first (1) (2) way is to initialize all the elements of the STR array to ' + ', and the (3) method is to initialize only the initial element of the STR array to ' \ s '. If the size of the array is very large, then the first two methods will have a significant overhead.
So, unless it is necessary (that is, we need to initialize all elements of the STR array to 0), we should use the first (3) way to initialize the string array.
1. Basic issues
An array can be initialized, that is, when defined, so that it contains values that the program can use immediately.
For example, the following code defines a global array and initializes it with a set of Fibonacci:
1 int iarray[10 ]={1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , ) // initialize 2 Main () 3 { 4 ...< /span>5 }
The value of the initialized array cannot be more than the number of array elements, and the value of the initialized array cannot be omitted by skipping the comma, which is allowed in C, but not in C + +.
For example, the following code is incorrect for initializing an array:
1 intarrayl[5]={1,2,3,4,5,6};//error-number of initialized values more than array elements2 intarray2[5]={1,,2,3,4};//Error: Initialization value cannot be omitted3 intarray3[5]={1,2,3,};//Error: Initialization value cannot be omitted4 intarray4[5]={};//error: Bad syntax format5 voidMain ()6 {7 //...8}
The number of initialized values can be less than the number of array elements. When the number of initialized values is less than the number of array elements, the preceding order initializes the corresponding value, followed by the initialization to 0 (global or static array) or to an indeterminate value (local array).
For example, the following program initializes an array:
1 //*********************2 //* * ch7_2.cpp * *3 //*********************4#include <iostream.h>5 6 intarray1[5]={1,2,3};7 Static intarray2[5]={1};8 9 voidMain ()Ten { One intarr1[5]={2}; A Static intarr2[5]={1,2}; - - intN; thecout <<"global:\n"; - for(n=0; n<5; n++) -cout <<" "<<Array1[n]; - +cout <<"\nglobal static:\n"; - for(n=0; n<5; n++) +cout <<" "<<Array2[n]; A atcout <<"\nlocal:\n"; - for(n=0; n<5; n++) -cout <<" "<<Arr1[n]; - -cout <<"\nlocal static:\n"; - for(n=0; n<5; n++) incout <<" "<<Arr2[n]; -cout <<Endl; to}
The result of the operation 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
example, the initialization of global arrays and global static arrays is done before the main function is run, while the initialization of local and local static arrays is done after entering the main function.
Global array arrayl[5] The values for the initialization table are sequentially initialized to two, and the values for the elements are initialized to 0 by default.
Global static array array2[5] As with the initialization of global arrays, the initialization table value (1) Represents the value of the 1th element, not all array elements are 1.
The local array arrl[5] is initialized sequentially based on the contents of the initialized table value, and because the initialization table value is only 1, the value of the 4 elements is indeterminate. Here are the values 23567.
The local static array arr2[5] is initialized sequentially based on the initialization table, and the values of the remaining 3 array elements are initialized to 0 by default.
2. Initialize character array
There are two ways to initialize an array of characters, one of which is:
Char array[10]={"Hello"};
The other is:
Char array[10]={' h ', ' e ', ' l ', ' l ', ' n '};
The first method is widely used, and when initialized, the system automatically fills in the position where the array has no value. In addition, the curly braces in this method can be omitted, that is, can be expressed as:
Char array[10]= "Hello";
The second method initializes the array one element at a time, as if an array of integers is initialized. This method is typically used to enter invisible characters that are not easily generated on the keyboard.
For example, the following code initializes a value of several tabs:
Char charray[5]={' \ t ', ' \ t ', ' \ t ', ' \ t ', ' n ');
Don't forget to allocate space for the last, ' go '. If you are initializing a string "Hello", then there are at least 6 array elements for the array it defines.
For example, the following code initializes an array, but causes an unexpected error:
Char array[5]= "Hello";
This code does not cause compilation errors, but is dangerous because it overwrites memory cells outside of the array space.
3. Omit array size
An array definition with initialization can omit the size of the array in square brackets.
For example, the following code in the array is defined as 5 elements:
int a[]={2,4,6,8,10};
The size of the array must be known at compile time. In general, the number in square brackets when declaring an array determines the size of the array. With an array definition initialized and omitting the size of the array in square brackets, the compiler counts the number of elements between curly braces in order to size the group.
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 benefits to having the compiler derive the size of the initialized array. It is often used to initialize an array of elements that are determined in the initialization, providing an opportunity for programmers to modify the number of elements.
How do you know the size of an array without having to specify the size of the array? The sizeof operation solves the problem. For example, the following code uses sizeof to determine the size of the array:
1 //*********************2 //* * ch7_3.cpp * *3 //*********************4 5#include <iostream.h>6 7 voidMain ()8 {9 Static inta[]={1,2,4,8, -};Ten for(intI=0; i< (sizeof(a)/sizeof(int)); i++) Onecout <<a[i] <<" "; Acout <<Endl; -}
The result of the operation is:
1 2 4) 8 16
The sizeof operation causes the for loop to automatically adjust the number of times. If you want to remove or delete elements from the initialization of a collection of array A, you can simply recompile, and the rest of the content needs to be changed.
The amount of storage that each array occupies can be determined with sizeof operations! sizeof returns the number of bytes for the specified item. sizeof is commonly used in arrays to allow code to be ported between 16-bit machines and 32-bit machines:
For the initialization of a string, be aware that the actual allocated space size of the array is the number of characters in the string plus the end of the, ' + ', Terminator.
For example, the following code defines a character array:
1 //*********************2 //* * ch7_4.cpp * *3 //*********************4 5#include <iostream.h>6 7 voidMain ()8 {9 Charch[]=" How is You";Ten Onecout <<"size of array:"<<sizeof(CH) <<Endl; Acout <<"size of string:"<<strlen (" How is You") <<Endl; -}
The result of the operation is:
Size of Array:12
Size of String:ll
example, the array size is 12, and the string length is 11.
The omitted array size can only be in an array definition that has an initialization.
For example, the following code produces a compilation error:
int A[];//error: Array size not determined
In the case of defining an array, the compiler must know the size of the array anyway.
Summary of issues related to string array initialization