C++ Arrays are the data structures which can be used to store consecutive values of the same data types. C++ Arrays can be declared for all c++ data types viz., int, float, double, char, struct, char etc., All the values are stored in consecutive memory locations. The values can be accessed by using the position of the stored value.
The Computer also stores C++ arrays in consecutive memory locations viz., 0,1,2 etc., This makes the C++ arrays’ accessibility faster. Unlike other data structures like Linked list, queue etc., the access to the elements is very fast.
C++ Arrays can be multi-dimensional. Most cases a maximum of 3 dimensions will suffice for programming. That too single dimension c++ arrays are the commonest.
Declaring C++ Arrays of int type:
The c++ arrays are declared with the data type name and the number of elements inside the square brackets.
int var_name[50]; // C++ Array of type int with maximum size = 50.
The above declaration means, the var_name is an array of integer type. The values inside this var_name array can be accessed by referring to the position of their elements like var_name[0], var_name[1] etc.,
All the C++ arrays are based on Zero index values. That means the position reference starts at 0 and counts till the n-1th position. So in the above array, 50 elements can be stored starting from 0 to 49. The maximum number of elements that can be stored or the maximum size of the c++ array is 50. The values can be stored and accessed as given in the following code snippet.
var_name[0] = 0; var_name[1] = 1; var_name[2] = 30; printf("Position 0: %d, Position 1:%d, Position 2:%dn", var_name[0], var_name[1], var_name[2]);
Care should be taken while using or referring to the position of the element. The position reference should not exceed the maximum size specified in the declaration. If it exceeds, the program will not generate any compiler errors. But it will raise run time errors and exceptions.
Declaration of a two dimensional array is also similar. A great example for a two dimensional c++ integer array will be matrix.
int array_matrix[3][3]; //Matrix of 3 * 3 type using c++ array of int data type
Declaring C++ arrays of type float:
The declaration and use of c++ arrays in float type are the same as int. The declaration is written as follows.
float var_float[100]; //A c++ array of float type
The above declaration means, the c++ float array contains 100 elements starting from position 0 till 99. The elements can be accessed as var_float[0], var_float[1], var_float[2] .. var_float[99].
var_float[0] = 10.05; var_float[1] = 11.04; var_float[99] = 87.65;
Declaring C++ arrays of type char:
This is one of the most widely used and problematic type of c++ array. When an array of char is declared, this char array can be used as a string with the maximum size as specified in the array declaration. When this c++ char array is declared with two dimensions, this can be thought as an array of strings.
char var_char_array[50]; //Memory reserved for 50 characters. char var_char_init[] = "c++ char array string example"; // initialization without specifying the size
Like the above the c++ char array can be declared with or without initializing values.
C++ Array Advantages:
- Easier to declare and use.
- Can be used with all data types.
C++ Array Disadvantages:
- Fixed size data. If the number of elements stored are less than the maximum size, then the rest of the memory space goes waste.
- If the number of elements to be stored are more than the maximum size, the array cannot accommodate those new values.