Coupon Accepted Successfully!


Array Initialization

  • C++ provides the facility of array initialization at the time of declaration.

  • The arrays are initialized in the same way as other variables are.

  • The general format is

Type array_name[row][column]={value list};

The value list holds the values of the array.
  • For example

int a[10][10][10]={{1,2,3},{1,3,4}.........}

  • If the values are of string or character it should be declared with its appropriate quotation.
  • For example

char string[10]={'c','o','m','p','u','t','e','r','\0'};

  • In the above example the last character (\0) is called as a null character, which tells that the string is terminated and no values are preceded further.

Unsized Array Initializations

  • The initialization of simple variables in their declaration has already been covered.

  • An array can be initialized in a similar manner.

  • In this case the initial values are given as a list enclosed in curly brackets.

  • For example initializing an array to hold the first few prime numbers could be written as follows:

int primes[] = {1, 2, 3, 5, 7, 11, 13};
In the above example the size of the array is not provided, so that the size can be declared during compile time.
Note: The array declaration with out the size of the array is called as variable size declaration.

Test Your Skills Now!
Take a Quiz now
Reviewer Name