# Thread: Initializing a 2D Array in C

1. ## Initializing a 2D Array in C

Hey guys,

I am trying to initialize a 2D array in C. For example, I have the matrix:

0 1 0 0
0 1 1 0
1 0 1 1
0 1 1 1

Now I think this would work:

Code:
```int A[4][4];

A[0][0] = 0;
A[0][1] = 1;
A[0][2] = 0;
A[0][3] = 0;
A[0][4] = 0;
A[1][1] = 0;
A[1][2] = 1;
... ETC....
A[4][4] = 1;```
But this is a real pain. I will have larger matrices like 17x17 and doing this would be a real annoyance.

Is there a way to do something like:
Code:
`int  A[4][4] = {0 1 0 0; 0 1 1 0, ... etc}`
?

Thanks!

2. Code:
`A[3][4] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };`

3. Or, for larger 2D arrays . . .

Code:
```int array[100][100];
int x, y;
for(x = 0; x < 100; x ++) {
for(y = 0; y < 100; y ++) array[x][y] = 0;
}```
Better than a few thousand chars of static initializers

4. Originally Posted by Nightowl
Or, for larger 2D arrays . . .

Code:
```int array[100][100];
int x, y;
for(x = 0; x < 100; x ++) {
for(y = 0; y < 100; y ++) array[x][y] = 0;
}```
Better than a few thousand chars of static initializers
But if you are setting all elements to zero, just doing
Code:
`int array[100][100] = { {0 } };`
works just fine.

--
Mats

5. Good point. Though the for loop can be used at non-initialization times, at any point in the program. And, to me, the for loop is a bit clearer.

6. You'll learn malloc() next and it'll shine a whole new light on this

7. Originally Posted by shiryu3
You'll learn malloc() next and it'll shine a whole new light on this
shiryu, that is overly condescending. Also, you should specify who you are talking to . . .

And yeah, you are correct. That is the code I use for allocated arrays.

8. Thank you for the answers, guys.

However, for this code:

Originally Posted by Nightowl
Or, for larger 2D arrays . . .

Code:
```int array[100][100];
int x, y;
for(x = 0; x < 100; x ++) {
for(y = 0; y < 100; y ++) array[x][y] = 0;
}```
Better than a few thousand chars of static initializers
This would just fill the matrix with zeroes. How can this be used to fill a matrix with specific values?

Also, I do know about malloc(). I don't know how I can use it to create a matrix with specified values, though. I also don't see what benefits it has over what brewbuck mentioned in his post.

I know there are plenty of ways to create a matrix filled with 0s, but I'd like to see how there can be a more efficient way to fill a matrix with specific values than what brewbuck posted.

Thanks again for the help.

9. > but I'd like to see how there can be a more efficient way to fill a matrix with specific values than what brewbuck posted.

Nope. Nothing more efficient than that, as far as initialisation/setting goes.

10. Originally Posted by Cell
... but I'd like to see how there can be a more efficient way to fill a matrix with specific values than what brewbuck posted...
A more efficient way would be to use the pointer and offset version.

11. Originally Posted by itCbitC
A more efficient way would be to use the pointer and offset version.
more effitient in what way?

12. Think on it. Having two loops rather than one? . . .
Code:
```int array[100][100];
int x;
for(x = 0; x < sizeof(int)*100*100; x ++) *(array+x) = 0;```
. . . should be faster than what I posted before.

Just more confusing, that's all . . .

13. Originally Posted by Nightowl
Think on it. Having two loops rather than one? . . .
Code:
```int array[100][100];
int x;
for(x = 0; x < sizeof(int)*100*100; x ++) *(array+x) = 0;```
. . . should be faster than what I posted before.

Just more confusing, that's all . . .
things like that should not be "thinked" - or exemine asm code or - better - use profiler.

I suppose both loops will be replaces by memset and will have same time exactly

14. If using a good compiler such as gcc, with all optimizations turned on, certainly. However, I don't think that gcc would do that kind of level of optimization by default . . .

Besides, the second one is probably almost exactly how memset() is implemented . . .

15. Originally Posted by Nightowl
If using a good compiler such as gcc, with all optimizations turned on, certainly. However, I don't think that gcc would do that kind of level of optimization by default . . .

Besides, the second one is probably almost exactly how memset() is implemented . . .
why to bother about performance is you do not turn optimization on?

if you debug something- you'd like to have most readable code, not the quickets, step-by-step has no gain from speed here.

and I doubt that memset is written with the loop like this - asm could move chunks of bytes longer than int in one instruction...