3

In C Language i'm creating a array ( 2 Dimensional ) in which all the elements are zeros

I do it the following way :

int a[5][5],i,j;  //a is the required array
for(i=0;i<5;i++)
   for(j=0;j<5;j++)
       a[i][j]=0;

I know some other way also :

int a[5][5]={0};

Are both the same or is there any difference ??

What should be preferred ??

Thank you !

Aditya Vikas Devarapalli
  • 3,186
  • 2
  • 36
  • 54
  • 3
    possible duplicate of [How to initialize an array in C](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c) – Sam Nov 20 '13 at 17:38
  • 1
    Note that the latter approach (suing view initialisers then elements) would only work for `0`. – alk Nov 20 '13 at 17:45

4 Answers4

6

The second method is more concise. Also consider:

memset(&a, 0, sizeof(a));
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
6

Both ways have the same effect, but the second one will generally be faster because it allows the compiler to optimise and vectorise that code.

Another widely accepted way (also optimisable) is

memset(a, 0, sizeof(a));
Sergey L.
  • 21,822
  • 5
  • 49
  • 75
3

Second one is useful. The first one uses for loop, so it takes time. There are other ways in which you can initialize an arrray...

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }; // All elements of myArray are 5
int myArray[10] = { 0 };    // Will initialize all elements to 0
int myArray[10] = { 5 };    // Will initialize myArray[0] to 5 and other elements to 0
static int myArray[10]; // Will initialize all elements to 0
/************************************************************************************/
int myArray[10];// This will declare and define (allocate memory) but won’t initialize
int i;  // Loop variable
for (i = 0; i < 10; ++i) // Using for loop we are initializing
{
    myArray[i] = 5;
}
/************************************************************************************/
int myArray[10] = {[0 ... 9] = 5}; // This works in GCC
memset(myArray, 0, sizeof(myArray));
  • `[0 ... 9] = 5` also also valid in other compilers. This works for me in `clang` and `icc` with `-std=gnu99` – Sergey L. Nov 20 '13 at 17:41
0

I would prefer latter one if I do not want to over stress my eyes (and my compiler too).

haccks
  • 104,019
  • 25
  • 176
  • 264
  • How do you over-stress a compiler? – wjmolina Nov 20 '13 at 17:40
  • 1
    Answer the question, and I will remove my down-vote if you can enlighten me. – wjmolina Nov 20 '13 at 17:41
  • 1
    A smart compiler is going to convert the first example into the second example. haccks is trying to be a bit irreverent, but he has a point that in the end, the object code is probably going to look the same. I would prefer the latter if I do not want to over stress *me*. :) – Mark Lakata Nov 20 '13 at 17:47
  • @JosuéMolina; I don't know how can I enlighten you. I answered what I know about that – haccks Nov 20 '13 at 18:02
  • @MarkLakata; I am running out of vote now otherwise I upvoted your comment. Regarding your line: ** haccks is trying to be a bit irreverent**, I think I was reverent!. If I answered like: **You should prefer latter if you do not want to over stress your compiler** then it would be irreverent :) – haccks Nov 20 '13 at 18:14
  • I see. I have removed my down-vote due to @MarkLakata's justification. Edit: my vote is locked. :-( Edit your answer, haccks. – wjmolina Nov 20 '13 at 18:14