Assigning required value to a variable before processing is called initialization. As we initialize a variable to the required value (for example, a=10), we can also initialize the individual elements of a two dimensional array. The initialization can be done at the time of declaration with the following syntax:
data_type array_name [exp1][exp2]={
{a1, a2, a3, ……, an},
{b1, b2, b3, ……, bn},
{c1, c2, c3, ……, cn},
.…………………………
};
Where
int arr[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
The declaration indicates that array arr has 3 rows and 3 columns. The pictorial representation of the two dimensional array is shown below:
Note that a[0][0] = 1 and so on with increment the value of square brackets.
Example: int arr[3][3] = {(1,2),(3,4),(5,6),(7,8)};
The declaration indicates that the array arr has 3 rows and 3 columns in which only first column of each row are initialized. The 3rd column of each row bill be initialized with zeros automatically as shown in the following figure:
i.e., all arr[0][2], arr[1][2], arr[2][2] are initialized to zeros.
Example: Consider the partial initialization shown below:
int arr[3][3] = {{1,2,3,4},{5,6},{7,8}};
In this array each row should have three columns. But, in this example, we have initialized row 0 with 4 elements. This results in syntax error.
Memory Map: To represent the memory map of two dimensional array, consider the following two dimensional array declaration:
int arr[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
Even though we represent a matrix pictorially as a two dimensional array, in memory they are stored contiguously one after the other. Assume that address of first byte of arr (known as base address) is 12000 and size of integer is 2 byte. The memory map for the two dimensional array arr is shown in below figure:
data_type array_name [exp1][exp2]={
{a1, a2, a3, ……, an},
{b1, b2, b3, ……, bn},
{c1, c2, c3, ……, cn},
.…………………………
};
Where
- data_type can be int, float, char, etc.
- array_name is the name of the array
- exp1 and exp2 are enclosed within square brackets
- a-1 to a-n are the values assigned to 1st row, b1 to bn are the values signed to 2nd row and so on.
Initializing all specified memory locations
Consider the initialization statement shown below:int arr[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
The declaration indicates that array arr has 3 rows and 3 columns. The pictorial representation of the two dimensional array is shown below:
Note that a[0][0] = 1 and so on with increment the value of square brackets.
Partial array initialization
If the numbers of values to be initialized are less than the size of the array, then the elements are initialized from left to right one after other. The remaining locations will be initialized to zero automatically.Example: int arr[3][3] = {(1,2),(3,4),(5,6),(7,8)};
The declaration indicates that the array arr has 3 rows and 3 columns in which only first column of each row are initialized. The 3rd column of each row bill be initialized with zeros automatically as shown in the following figure:
i.e., all arr[0][2], arr[1][2], arr[2][2] are initialized to zeros.
Example: Consider the partial initialization shown below:
int arr[3][3] = {{1,2,3,4},{5,6},{7,8}};
In this array each row should have three columns. But, in this example, we have initialized row 0 with 4 elements. This results in syntax error.
Memory Map: To represent the memory map of two dimensional array, consider the following two dimensional array declaration:
int arr[3][3] = {
{1,2,3},
{4,5,6},
{7,8,9}
};
Even though we represent a matrix pictorially as a two dimensional array, in memory they are stored contiguously one after the other. Assume that address of first byte of arr (known as base address) is 12000 and size of integer is 2 byte. The memory map for the two dimensional array arr is shown in below figure:
Tidak ada komentar:
Posting Komentar