1

What does {0} mean below?

An array of one zero char? But I don't think you can initialize a larger array with a smaller array, or can you? And it would be int, not char, so would cause at least a warning, right? Or am I misunderstanding it completely? Does this result in buffer's first character being zeroed out?

struct ring_buffer
{
    unsigned char buffer[SERIAL_BUFFER_SIZE];
    volatile unsigned int head;
    volatile unsigned int tail;
};

ring_buffer rx_buffer  =  { { 0 }, 0, 0 };
Irina Rapoport
  • 1,404
  • 1
  • 20
  • 37
  • `""` would have done the same job in this case. – Ja͢ck Mar 28 '14 at 05:04
  • The inner `{0}` seems really to be expressed more for the benefit of the reader/maintainer, making explicit what would implicitly already be the case if the entire RHS of the expression consisted only of `{0}`, instead of `{{0},0,0}`. – DavidO Mar 28 '14 at 05:07
  • " I don't think" -- maybe think less and read the documentation more? "would cause at least a warning" -- do you expect `char c = 'x';` to produce a warning? `'x`' is an int in C. – Jim Balter Mar 28 '14 at 05:36
  • 2
    @JimBalter, having a bad day? – Irina Rapoport Mar 28 '14 at 17:28

5 Answers5

3

{0} means "zero-init" this struct or array. In the example above, rx_buffer.buffer will be filled with all zero bytes instead of whatever happened to be on the stack at that moment.

The following should also work for intializing the entire ring_buffer struct to all zero bytes:

ring_buffer rx_buffer = {0};
selbie
  • 100,020
  • 15
  • 103
  • 173
  • `{0}` also will initialize pointers to be null pointers, so it is a good way to initialize any variable at all (including arrays and structures). – M.M Mar 28 '14 at 05:24
  • 1
    "{0} means "zero-init" this struct or array" -- it's not a special case ... `{1}` means "initialize the first item to 1 and initialize the rest of the items to zero" -- unspecified initializers are always 0. – Jim Balter Mar 28 '14 at 05:40
1

You initialize an array in this format: char array = { value1, value2, ...};

In C, if array (or structure) length is larger than the number of values you used to initialize then compiler automatically initializes the rest of the values to 0.

In the same way you can initialize structure also. In your case since the first element is an array it is initialized as an array to all 0.

Therefore, to explain: ring_buffer rx_buffer = { { 0 }, 0, 0 };

This will declare a structure variable rx_buffer of type struct ring_buffer and initialize all its elements to zero. Since the first element is itself an array, it is initialized the same way a regular array is initialized.

0xF1
  • 6,046
  • 2
  • 27
  • 50
1

It's mean zero init array of uchar (all elements 0) rx_buffer.buffer = {0}

rpc1
  • 688
  • 10
  • 23
0

But I don't think you can initialize a larger array with a smaller array, or can you?

If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type

This may help you How to initialize array to 0 in C?

Community
  • 1
  • 1
Muthe
  • 1
0

Initializing a variable with {0} means to fill it with zeros. It is a way to explicitly show to a reviewer of the code that the variable is zeroed. Your initialization is a message to other developers not to the compiler because static variables without initialization are initialized to 0 by default.

However, if it was followed by non zero values, it would have different meaning. For example:

struct ring_buffer {
   unsigned char buffer[SERIAL_BUFFER_SIZE];
   volatile unsigned int head;
   volatile unsigned int tail;
};

ring_buffer rx_buffer  =  { { 0 }, 1, 2 };

would mean that buffer is filled with zeros, head is initialized to 1 and tail to 2.

Probably the author of the code supposed that one day the code may be changed in such a way and made this explicit in order to avoid possible bugs in the future.

Marian
  • 7,402
  • 2
  • 22
  • 34