Quickest way to initialize an array of structures to all-0's?

后端 未结 7 636
无人及你
无人及你 2020-12-14 16:49

I\'m trying to initialize an array of structures to all-0\'s, using the below syntax:

STRUCTA array[MAX] = {0};

However, I\'m getting the f

7条回答
  •  温柔的废话
    2020-12-14 17:45

    You can avoid the warning by using completely empty braces:

    STRUCTA array[10] = {};

    The array will be aggregate-initialized, which means that each of the structs in it will in turn be value-initialized. Value-initialization with empty brackets turns into aggregate-initializion of each struct, which sets all fields to 0, which is what you want.

    This works in all cases as long as your structs are POD (see the links above for precise description).

提交回复
热议问题