Check The Programming Section
The general form for declaraing a 1-d array is
type var_name[size];
Like other variables an array must be declared before it used, so that compiler can allocates space for them in memory. In the above general form,
type represents a data type and tells the compiler what kind of values var_name can store. For example, int, float, and double.
var_name is the name of the array, and
size is the maximum number of value that the array can hold. The size must be a positive integer number
For example, to declare an array marks with 10 double elements the required syntax is
double marks[10];
Like other variables we can declare multiple arrays using one statement
double sub1_marks[5], sub2_marks[5];
In C/C++, various ways we can initialized an arary, with the declaration statement of the array.
double marks[5] = {4.5, 6.7, 7.8, 9.5, 7.4};
In the above initialization, the comma separated brace initialization {} is used to initialize the array. The number of values is equal to the size (5) of the array. While initializing an array with the same statement of the array declaration, specifying the size of the array is optional. Consider the statement mentined below:
double marks[] = {4.5, 6.7, 7.8, 9.5, 7.4};
Sometimes, there is a requirement of initializing an array with 0, if we do not wants any garbage value as array elements the below syntax can fulfill our requirement.
int age[10] = {0};
int age[10] = {};
Then all 10 elements of the array is initialized to 0. The most important point to remember, while initializing an array with 0, that we can not omit the size of the array. In this case, it is compulsory to specify the size of the array. Else size of the array is considered as 1. Consider the statement below:
int age[] = {};
For the above example, only first three elements of age is initialized by the assigned values specifies inside the curly braces and for others default value zero will be considered.
It is also possible, to initialize few elements of the array and for others default value 0 will be assigned by the compiler. The required syntax is:
int age[10] = {20, 45, 50};
For the above example, only first three elements of the age is initialized by the assigned values specifies inside the curly braces and for others default value 0 will be considered.
The amount of storage required to hold an array is directly related to its type and size. For an one-dimenstional array, the total size in bytes and computed as follows:
total bytes = sizeof(array type) x size of the arary;
Source code to calculate the required memory
#include<iostream>
using namespace std;
int main(){
int num[10];
int sizeInBytes = sizeof(int) * 10;
cout<<sizeInBytes;
}
In the above source code, type of num array is int (takes 4 byte of memory) and number of elements (size) of the array is 10. So the total memory requirement in bytes is 10 X 4 = 40.
It is very easy to know the number of elements or the size of the array using sizeof operator. The syntax is:
int size = sizeof(num)/sizeof(num[0]);
In the above syntax num[0] represents the first element of num and [0] is the index of the first element. Regarding the array indexing, it is discussed next. I don’t wants to make confusion for new programmer about the array declaration and initialization so decided to ends here.