Is there any difference between these ways to initialize a C array in C ++?

I want to initialize all elements of the array to zero or nullptr

struct Window{ int a;};


int main()
{
    Window* list[4] = { 0, 0, 0, 0 };
    Window* list2[4] = {0};
    Window* list3[4] = {};
    Window* list4[4]{ 0, 0, 0, 0 };
    Window* list5[4]{0};
    Window* list6[4]{};
}

I understand that when initializing at least one member for any value, all the others are initialized to zero, so if I do this:

int list[4] = { 6 };

The first member becomes 6, and all the rest are initialized to zero. I am confused, however, with:

int list[4]{0};

and

int list[4]{};

I assume that the empty square brackets immediately after the declaration without an equal sign is what is called initialization of zero, in contrast to the initialization by default, but there is one too int list[4]{0}, right? Is it connected std::initializer_listbehind the scenes or not? I thought they were used only for non-POD types, so why std::initializer_listnot here?

+6
2

C ++?

. .

, , int list [4] {0}, ?

. , int . , , int . .

std:: initializer_list ?

std::initializer_list .

+3

++, C style:)

. vector, . . Window.

struct Window {
    // Class constructor
    Window(const int _a_) {
       a = _a_;
    }

    // Class member 
    int a;
};

std::vector<Window> windows (4, Window(0));

4 Window a. , .

0

All Articles