SoFunction
Updated on 2025-03-11

C language indefinite length array and initialization method

C language does not support indefinitely long arrays, either malloc or dynamically specify its length.

Dynamic arrays cannot be initialized, you can use memset

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

num = 5;

arr[num];

If you use arr[], you need to initialize the following alignment to specify the length. Otherwise, the compilation can be passed, but by default, there is only one unit, which is more than one unit's length, and may be flushed during the run of the subsequent program.

int arr[] = {0};//Define an array of cells,Not an indefinitely long 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}; does not initialize all elements of a to 0, int a[256]={1}; does not initialize all elements of a to 1.

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

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

When the array is defined, the size of the array is not specified, and when the initialization is initialized with a list, the size of the array is determined by the number of list elements at the initialization. So v1 and v2 are int[4] and char[4] types respectively. If the array size is explicitly specified, an error will occur when the number of elements specified exceeds this size during initialization. For example:

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

If the number of elements specified during initialization is smaller than the size of the array, the remaining elements will be initialized to 0.

For example

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

Equivalent to

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

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

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

As the name suggests, aggregate is a collection of multiple things gathered together. This definition includes a collection of mixed types: like struct and class, etc., and an array is a collection of single types.

Initializing collections is often both lengthy and error-prone, while aggregate initialization in C++ becomes very convenient and safe. When generating a collection object, all you have to do is specify the initial value, and the initialization work will be undertaken by the compiler. This specification can be used in several different styles, depending on the type of collection being processed. But no matter what the case is, the specified initial value must be enclosed in braces.

For example, an internal type array can be defined like this:

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

If the initialization value is given more than the number of array elements, the compiler will give an error message. But what if the initialization given is less than the number of data elements?

For example:

int b[6] = {0};

At this time, the compiler will assign the first initialization value to the first element of the array, and then assign 0 to the remaining elements. Note that if an array is defined without giving a column of initial values, the compiler will not do the initialization work. So the above expression is a neat way to initialize an array to zero.

The above article about C language indefinite length array and initialization method is all the content I share with you. I hope you can give you a reference and I hope you can support me more.