Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing entire 2D array with one value

With the following declaration

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

I get the array with all zeroes but with the following one

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

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?

EDIT: I have just understood that using memset with value as 1, will set each byte as 1 and hence the actual value of each array cell wont be 1 but 16843009. How do I set it to 1?

like image 448
Kraken Avatar asked Mar 20 '13 10:03

Kraken


People also ask

How do you initialize a 2D array with a single value?

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 an array with an entire value?

Initializer List: To initialize an array in C with the same value, the naive way is to provide an initializer list. We use this with small arrays. int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index.

How do you initialize all elements in a 2D array?

char grid[row][col]; memset(grid, ' ', sizeof(grid)); That's for initializing char array elements to space characters.

How do you initialize all the elements of an 2D array to any specific value in Java?

The recommended approach to initialize an array with any value is using the Arrays. fill() method. For a 2D array, Arrays. fill() can be called for each array using a for-loop.


1 Answers

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".

like image 54
Lundin Avatar answered Sep 25 '22 17:09

Lundin