Initialization of all elements of an array to one default value in C++?

前端 未结 13 1267
礼貌的吻别
礼貌的吻别 2020-11-22 07:59

C++ Notes: Array Initialization has a nice list over initialization of arrays. I have a

int array[100] = {-1};

expecting it to be full with

13条回答
  •  星月不相逢
    2020-11-22 08:34

    There is an extension to the gcc compiler which allows the syntax:

    int array[100] = { [0 ... 99] = -1 };
    

    This would set all of the elements to -1.

    This is known as "Designated Initializers" see here for further information.

    Note this isn't implemented for the gcc c++ compiler.

提交回复
热议问题