Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a 2D-array at declarationtime in the C programming language

Tags:

arrays

c

How do I initialize a 2D array with 0s when I declare it?

double myArray[3][12] = ?

like image 369
Chris_45 Avatar asked Nov 06 '09 16:11

Chris_45


People also ask

How 2D array is initialized?

Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.

How do you initialize a 2D array at run time?

Like the one dimensional array, 2D arrays can be initialized in both the two ways; the compile time initialization and the run time initialization. int table-[2][3] = { { 0, 2, 5} { 1, 3, 0} }; This way is the best way to initialize the 2D array. It also increases the readability of the user.


1 Answers

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}}; 
like image 92
pmg Avatar answered Oct 11 '22 14:10

pmg