There might be a single way to define an array, but there is no one way when it come to the initialization of array.
C/C++ allows the initialization of array at the time of their declaration. The general form of initialization array is the same as of the variable initialization:
SYNTAX
[highlight color="green"]
data type[/highlight] array_name[size]={value_list};
The value of the list is separated by a simple comma. All of these value are of the same type as the data type of the array. The first value is placed in the first position of the array, the second in the second place, and so on. Note that a semicolon follows the }.
INITIALIZED ARRAY
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
This means arr[0] will have value 1 and arr[9] will have the value 10.
EXAMPLE
#include
using namespace std;
int main()
{
int arr[5]={1,2,3,4,5};
for(int i=0;i<5;i++){cout<<arr[i];}
}
If you don’t initialize it, the elements of the array will have a random values assigned to it.
That is one way to initialize an element of an array.
The video below explains the concept of initialization:
Administrador de ENGGDRCAOS. Canal dedicado especialmente a la formación del estudiante que aspira a ser ingeniero. Todos los videos son Ingles. Apasionado del universo Apple, estudiante de Ingeniería y Gamer por vocación.
