1

I have this:

uint8_t buf[X][Y];

I would like to initialize all elements to 0. Will this do the trick:

uint8_t buf[X][Y] = { 0 };

? i.e. will it initialize all X*Y elements to 0?

3
  • Have you tried it? Did it work? Did it work with { 1 }? Commented Aug 1, 2014 at 8:03
  • possible duplicate: stackoverflow.com/questions/15520880/… Commented Aug 1, 2014 at 8:04
  • To initialize zeros = {}; is enough. Commented Aug 1, 2014 at 8:05

1 Answer 1

2

Yes. If you have an initializer ({ ... }), all elements not explicitly initialized will be initialized to zero.

Edit: Removed part that is not correct when using C.

Sign up to request clarification or add additional context in comments.

2 Comments

That's valid C++, but C syntactically requires something between the braces. (C11 §6.7.9/1)
@Potatoswatter That is correct. Doesn't work with empty brackets.

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.