In the C language, the initialization of an array is in the following ways:
1. Initialize at the same time as defined:
int array[10] = {1,2,3,4,5};
2. Do not specify the size of the array at the time of definition, by initializing the array elements to determine the size:
int array[] = {1,2,3,4,5};
3. Define the variable and initialize it. Note: The size of the array must be set when defining the variable. You can only assign values to the member elements of the group variables at this time, and cannot be assigned in bulk.
int array[]; Error usage
int array[12];
Array = {1, 2, 3}; Error usage.
Array[0] = 1;
ARRAY[1] = 2;
ARRAY[2] = 3;
4. There is only one case in which the size of the array can be unspecified when it is the formal parameter of the method. This is the first address of the array, so the array length cannot be obtained inside the method, and the array length must be passed as a parameter to the function.
void Test ()
{
int array[] = {1,2,3,4,5};
int length = sizeof (array)/sizeof (int);
int result = Sumofarray (array, length);
printf ("%d", result);
}
int Sumofarray (int array[], int length)
{
int result = 0;
for (int i=0; i<length; i++)
{
Result + = Array[i];
}
return result;
}
Initialization of arrays in C language