double myArray[3][12] = {0};

or, if you want to avoid the gcc warning "missing braces around initializer" (the warning appears with -Wall or, more specifically -Wmissing-braces)

double myArray[3][12] = {{0}};
Answer from pmg on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-initialize-2d-array-in-c
Initialization of 2D Array in C - GeeksforGeeks
1 week ago - A 2-D array can be initialized by providing a list of values enclosed in braces {}, where each value is assigned to an array element.
Discussions

How to initialize a 2d array in C - Stack Overflow
I am trying to figure out a way to initialize my 2d string array and Iwanted to know if there is a better way of doing it than what I have coded below. Is there also a way to not provide the size o... More on stackoverflow.com
🌐 stackoverflow.com
c - Initializing entire 2D array with one value - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I don’t get the array with all one value. The default value is still 0. Why this behavior and how can I initialize with all 1? More on stackoverflow.com
🌐 stackoverflow.com
Initializing a 2d array in C - Stack Overflow
Copyint main() { int x, y; int *xptr, *yptr; int array[10][10]; int j; int k; int z = 0; for(j = 0; j < 10; j++) { for(k = 0; k < 10; k++) { array[j][k] = j * 10 + k; } } xptr = &array[0][0]; for(j = 0; j < 10; j++) { for(k = 0; k < 10; k++) { printf("array[%d][%d] = %d \n", j, k, *(xptr + j), (xptr + k)); } } system("PAUSE"); } I am trying to initialize a 2d ... More on stackoverflow.com
🌐 stackoverflow.com
Initialize a 2D array with a single value?
let mut my_array: [[u32; 21]; 21] = [[0; 21]; 21]; More on reddit.com
🌐 r/rust
9
7
June 4, 2015
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › initialize-multidimensional-array-in-c
Initialization of Multidimensional Array in C - GeeksforGeeks
December 18, 2024 - Explanation: In this example, we declare a 2x2 2D array a 2x2x2 3D array. We initialize it with values for each element. We can see that the values are nested in braces {} according to the number of rows and columns.
🌐
PrepBytes
prepbytes.com › home › c programming › two dimensional array in c
Two Dimensional Array in C - Syntax, Declaration & Examples
December 28, 2023 - In this blog section, we will explain the syntax of the two dimensional array in c. ... array_name: It will tell the name of the array with which it will be referenced in the whole program. ... The array will have a total of m x n elements. For example, the 2D array in c of 20 rows and 10 columns will be declared like this.
🌐
CodinGeek
codingeek.com › home › 2d arrays in c language - how to declare, initialize and access elements
2D Arrays in C - How to declare, initialize and access
April 11, 2021 - In this C programming tutorial, we will discuss how to declare, initialize, access, and iterate over two-dimensional arrays and implement a program using 2D arrays.
🌐
Stack Overflow
stackoverflow.com › questions › 72743106 › how-to-initialize-a-2d-array-in-c
How to initialize a 2d array in C - Stack Overflow
Which way(s) are best depends on circumstances, such as how the array will be used. Initializing the first byte of each (outer) array element to the null character might suffice. But using a separate counter to count how many array elements are in use might be better.
🌐
Quora
quora.com › How-do-I-initialize-a-2D-array-in-C
How to initialize a 2D array in C - Quora
Answer (1 of 2): Two basic ways. One general, one that work in more spesific context. The general basic way is to loop over all elements in the array in a double loop, initializing each element. The other way is to use calloc for zero initialized memory in an aproperiate way for a heap based 2d...
Find elsewhere
🌐
Scaler
scaler.com › home › topics › c multidimensional arrays (two dimensional array in c)
Two Dimensional Array in C | Multidimensional Array in C - Scaler Topics
May 1, 2024 - Here, dataType can be any valid C data type, arrayName is your chosen identifier for the array, and numberOfRows and numberOfColumns define the array's size. This blueprint is crucial for preparing your data storage with precision. Once declared, initializing a 2D array breathes life into it, ...
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › 2d-arrays-in-c-example
Two dimensional (2D) arrays in C programming with example
We already know, when we initialize a normal array (or you can say one dimensional array) during declaration, we need not to specify the size of it. However that’s not the case with 2D array, you must always specify the second dimension even if you are specifying elements during the declaration.
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_multi_dimensional_arrays.htm
Multi-dimensional Arrays in C
The first square bracket always represents the dimension size of rows, and the second is the number of columns. Obviously, the array has 3 X 5 = 15 elements. The array is initialized with 15 comma−separated values put inside the curly brackets.
🌐
Programiz
programiz.com › c-programming › c-multi-dimensional-arrays
C Multidimensional Arrays (2d and 3d Array)
You can initialize a three-dimensional array in a similar way to a two-dimensional array. Here's an example, int test[2][3][4] = { {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}}; // C program to store temperature of two cities of a week and display it. #include <stdio.h> const int CITY = 2; const int WEEK = 7; int main() { int temperature[CITY][WEEK]; // Using nested loop to store values in a 2d array for (int i = 0; i < CITY; ++i) { for (int j = 0; j < WEEK; ++j) { printf("City %d, Day %d: ", i + 1, j + 1); scanf("%d", &temperature[i][j]); } } printf("\nDisplaying values: \n\n"); // Using nested loop to display vlues of a 2d array for (int i = 0; i < CITY; ++i) { for (int j = 0; j < WEEK; ++j) { printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]); } } return 0; }
🌐
StudyMite
studymite.com › blog › two-dimensional-array-in-c
2d array in C | Initialisation and Program | StudyMite
2 weeks ago - In C, array indices always start ... A[r, c]. In C, this uses the comma operator and will not work as expected. Always use separate brackets: A[r][c]. You can initialize a 2D array at the time of declaration in several ...
🌐
Vaia
vaia.com › 2d array in c
2d Array in C: Definition & Examples | Vaia
December 12, 2024 - This makes operations like iteration and data assignment easier and more intuitive.The syntax for declaring a 2D array in C is as follows: 'data_type array_name[row_size][column_size];' The data_type specifies the type of data the array holds, such as int, char, or float.
Top answer
1 of 7
157

You get this behavior, because int array [ROW][COLUMN] = {1}; does not mean "set all items to one". Let me try to explain how this works step by step.

The explicit, overly clear way of initializing your array would be like this:

#define ROW 2
#define COLUMN 2

int array [ROW][COLUMN] =
{
  {0, 0},
  {0, 0}
};

However, C allows you to leave out some of the items in an array (or struct/union). You could for example write:

int array [ROW][COLUMN] =
{
  {1, 2}
};

This means, initialize the first elements to 1 and 2, and the rest of the elements "as if they had static storage duration". There is a rule in C saying that all objects of static storage duration, that are not explicitly initialized by the programmer, must be set to zero.

So in the above example, the first row gets set to 1,2 and the next to 0,0 since we didn't give them any explicit values.

Next, there is a rule in C allowing lax brace style. The first example could as well be written as

int array [ROW][COLUMN] = {0, 0, 0, 0};

although of course this is poor style, it is harder to read and understand. But this rule is convenient, because it allows us to write

int array [ROW][COLUMN] = {0};

which means: "initialize the very first column in the first row to 0, and all other items as if they had static storage duration, ie set them to zero."

therefore, if you attempt

int array [ROW][COLUMN] = {1};

it means "initialize the very first column in the first row to 1 and set all other items to zero".

As for how to initialize the whole array to a specific value/values, see https://stackoverflow.com/a/13488596/584518.

2 of 7
56

If you want to initialize the array to -1 then you can use the following,

memset(array, -1, sizeof(array[0][0]) * row * count)

But this will work 0 and -1 only

🌐
DigitalOcean
digitalocean.com › community › tutorials › two-dimensional-array-in-c-plus-plus
2D Arrays in C++: Declare, Initialize & Operations | DigitalOcean
1 month ago - In this case too, arr is a 2D array with 4 rows and 2 columns. While this syntax is correct, it is generally considered less readable and can be prone to errors, especially for larger arrays. For better clarity and maintainability, it is highly recommended to use nested curly braces to visually separate the rows. One more thing worth knowing: if you provide fewer initializers than the array can hold, the remaining elements are automatically set to zero.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › multidimensional-arrays-in-c
Multidimensional Arrays in C - 2D and 3D Arrays - GeeksforGeeks
2 weeks ago - For example, we can declare a two-dimensional integer array with name 'arr' with 10 rows and 20 columns as: ... We can initialize a 2D array by using a list of values enclosed inside '{ }' and separated by a comma as shown in the example below:
🌐
Stack Overflow
stackoverflow.com › questions › 12503904 › initializing-a-2d-array-in-c
Initializing a 2d array in C - Stack Overflow
Copyint main() { int x, y; int *xptr, *yptr; int array[10][10]; int j; int k; int z = 0; for(j = 0; j < 10; j++) { for(k = 0; k < 10; k++) { array[j][k] = j * 10 + k; } } xptr = &array[0][0]; for(j = 0; j < 10; j++) { for(k = 0; k < 10; k++) { printf("array[%d][%d] = %d \n", j, k, *(xptr + j), (xptr + k)); } } system("PAUSE"); } I am trying to initialize a 2d ...
🌐
Quora
computerscienceevolution.quora.com › https-www-quora-com-How-do-I-initialize-a-2D-array-in-C-answer-Rune-Valle
https://www.quora.com/How-do-I-initialize-a-2D-array-in-C/answer/Rune-Valle
The general basic way is to loop over all elements in the array in a double loop, initializing each element. The other way is to use calloc for zero initialized memory in an aproperiate way for a heap based 2d array.