declare and initialize 2d array in c code example
Example 1: how to declare multidimensional array in c
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
Example 2: c fill 2d array
// Either
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
// Or
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};
// OR
int i, j;
for (i = 0; i < HEIGHT; i++) { // iterate through rows
for (j = 0; j < WIDTH; j++) { // iterate through columns
disp[i][j] = disp[i][j];
}
}
Example 3: double array in c
int disp[2][4] = {
{10, 11, 12, 13},
{14, 15, 16, 17}
};
Example 4: 2D Array In C
// Array of size n * m, where n may not equal m
for(j = 0; j < n; j++)
{
for(i = 0; i < m; i++)
{
array[i][j] = 0;
}
}