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 !

2
  • 3
    possible duplicate of How to initialize an array in C Commented Nov 20, 2013 at 17:38
  • 1
    Note that the latter approach (suing view initialisers then elements) would only work for 0. Commented Nov 20, 2013 at 17:45

4 Answers 4

6

The second method is more concise. Also consider:

memset(&a, 0, sizeof(a));
Sign up to request clarification or add additional context in comments.

Comments

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));

5 Comments

Don't forget to take the address of a (&a).
@BitFiddlingCodeMonkey both a and &a have the same meaning in C when a is declared as an array.
@SergeyL.; No. This is not true. Although their values are same but have different meaning.
Actually, you may be right @SergeyL. a == a[0] == &a[0][0], right?
@BitFiddlingCodeMonkey Yeah, value is what I meant. The types will be different, but the value is the same - the start of the array = address of the first element.
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));

1 Comment

[0 ... 9] = 5 also also valid in other compilers. This works for me in clang and icc with -std=gnu99
0

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

6 Comments

How do you over-stress a compiler?
Answer the question, and I will remove my down-vote if you can enlighten me.
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. :)
@JosuéMolina; I don't know how can I enlighten you. I answered what I know about that
@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 :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.