# Thread: Initializing a 2D Array in C

1. Point. And yes, the first example, the nested for loops are the most readable, IMO. Besides a call to memset(), as you point out so nicely.

Note that optimizations really mess up GDB, however. If you're working on a project that has lots of calculations to go through before it reaches a breakpoint, speed is always nice to have.

2. Originally Posted by Cell
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!
for decently small arrays(4x4 or 5x5) like the ones you'hv shown
a[4][4]={{1,2,3,4},{3,4,5,6}....} is appropriate enough...

however when you go for huge arrays like the SBOX of Triple-DES or AES algorithm then you'd find it painful to show SBOXes in the same fashion as shown above...

You'd be much better off passing all of them as single dimentional array...
such as,
sbox_des[8][4][16]={14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7,0,15...... ...}. the compiler would automatically parse the array grouping first 64 elements as sbox[0] and then 4 sets of 16 elements...
just make sure that you enter the values row-wise , one row at a time, before going to the next row...and you'll be fine...

this approch is not recommended if someone else would want to examine the array, however it is acceptable if the array is of know constants, whose values are never to be manipulated...such as the SBOXes...

3. Originally Posted by vart
more effitient in what way?
It's easier on the compiler to translate a pointer and offset compared to array indexing.

4. Originally Posted by itCbitC
It's easier on the compiler to translate a pointer and offset compared to array indexing.
I do not think it necessary to facilitate the work of the compiler, if it makes the code less readable and gives no other benefits, such as performance

5. Originally Posted by vart
I do not think it necessary to facilitate the work of the compiler, if it makes the code less readable and gives no other benefits, such as performance
That would be true only for the uninitiated, besides it's not the work of the compiler but the choice of the programmer.

6. Originally Posted by itCbitC
That would be true only for the uninitiated, besides it's not the work of the compiler but the choice of the programmer.
In my opinion, a programmer who chooses to use pointer notation with an offset over the equivalent array notation to iterate over an array is uninitiated. It is a different story if a programmer chooses to use pointers over array indices to iterate over an array. (It is still true that a modern optimising compiler would optimise away the difference, but for the latter case it is the choice between an iterator and an index, not merely a choice between pointer notation and array index notation.)