Explain static And dynamic initialization of an one and two dimensionsal array with example
Answers
Explanation:
one dimensional array-
There are two ways to initialize an array.
Static array initialization - Initializes all elements of array during its declaration.
Example of static initialization-
int rollno[5] = {90, 86, 89, 76, 91};
int rollno[] = {90, 86, 89, 76, 91};
Dynamic array initialization - The declared array is initialized some time later during execution of program.
Example of dynamic initialization-
marks[0] = 90; // Assigns 90 to first element of marks array
marks[1] = 86; // Assigns 86 to second element of marks array
...
...
...
marks[4] = 91;
Two dimensional Array-
Example -
int matrix[4][3] = {
{10, 20, 30}, // Initializes matrix[0]
{40, 50, 60}, // Initializes matrix[1]
{70, 80, 90}, // Initializes matrix[2]
{100, 110, 120} // Initializes matrix[3]
};