Puedes seguirnos en

BUSCADOR

Engineering

Initialize the elements of an Array in C

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 }.

Anuncio publicitario

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;

Anuncio publicitario

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:

Anuncio publicitario
Escrito por

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.

Publicidad

ARTÍCULOS RELACIONADOS

Ciencia y tecnología

Desde hace algunos días tenemos disponible para su instalación el nuevo sistema de Google, Android 4.3. Por el momento los primeros terminales en recibir...

Linux

Cool Reader es un visor de e-book compatible con una larga serie de formatos de documentos. Cuenta con una paleta personalizable, cambio de formato...

Ciencia y tecnología

Instale shotcut Video Editor en Ubuntu 13.04, 12.04, 12.10  Este tutorial sencillo y breve le mostrará cómo instalar Vídeo Editor shotcut en Ubuntu. Shotcut...

Ciencia y tecnología

PhotoFilmStrip es una estupenda herramienta para todos los públicos, en la que en pocos pasos podemos pasar nuestras fotografías a vídeo. Veamos su manejo...