C language indefinite array and initialization methods

  • 2020-06-07 04:56:57
  • OfStack

The C language does not support an indefinite array, either malloc or dynamically specifying its length

Dynamic array cannot be initialized, memset can be used

1.int* p = (int*)malloc(num);

2.int num = 5;


If arr[] is used, it needs to be initialized with the back alignment to specify the length, otherwise the compilation will pass, but by default there is only 1 unit, and more than 1 unit may be washed out later in the program

int arr[] = {0} ; // Defines the 1 An array of 20 units, not an indefinite array 

It is better not to use arr[] = {0} to define an array. You should specify the length before defining an array

int arr[256] = {0};

int a [256] = {0}; Instead of initializing all elements of a to 0, int a[256]={1}; Nor do you initialize all elements of a to 1.

An array can be initialized with a column value, for example

int v1[] ={1,2,3,4};
char v2[]={'a','b','c',0};

When an array is defined without specifying a size, and when initialized with a list, the size of the array is determined by the number of list elements at initialization time. Therefore, v1 and v2 are of type int[4] and char[4], respectively. If the array size is explicitly specified, an error occurs when the number of elements specified at initialization time exceeds this size. Such as:

char v3[2] ={'a','b',0}; // Error: Too many initializations 
char v3[3] ={'a','b',0}; // correct 

If the number of elements specified at initialization time is less than the array size, the remaining elements are initialized back to 0.

For example,

int v5[8]={1,2,3,4};

Is equivalent to

int v5[8]={1,2,3,4,0,0,0,0};

Note that there is no array assignment in the form of:

void f()
 v4={'c','d',0}; // Error: not an array assignment 

As the name implies, a collection (aggregate) is a collection of several things at a time. This definition includes a collection of mixed types: for example, struct and class, an array is a collection of a single type.

While initialization of a collection is often verbose and error-prone, in C++ (aggregate initialization) it becomes convenient and secure. When a collection object is generated, all you have to do is specify the initialization value, and then the compiler does the initialization. This specification can be done in several different styles, depending on the collection type being processed. In either case, the specified initial value is enclosed in braces.

For example, an array of an internal type can be defined as follows:

int a[5] = { 1, 2, 3, 4, 5 } ; 

If the number of initialization values given is greater than the number of array elements, the compiler gives an error message. But what if the number of initializations given is less than the number of data elements?

Such as:

int b[6] = {0};

At this point, the compiler assigns the first initialization value to the first element of the array, and then 0 to the rest of the elements. Note that the compiler does not do the initialization if an array is defined without an initial value of 1 column. So the above expression is a neat way to initialize 1 array to zero.

Related articles: