Initializing array question

I have been studying C++ for some time now and I recently encountered a form of initializing an array that has me scraching my head.

CUSTOMVERTEX vertices[]
{
{ 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLORT_XRGB(0, 0, 255), },
{ 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
}

If I understand well this declares and initializes a 1 dimensional array of
type CUSTOMVERTEX (which is a struct) and it contains 3 elements.

What throws me off is the last two commas at the end of each element initializer!
I don't understand the syntax (from the point of view of my limited knowledge, of course!).
Thanx for any help understanding!!
Trailing commas in baced-init-list are allowed and discarded. Your code is parsed as:
1
2
3
4
5
CUSTOMVERTEX vertices[] {
    { 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLORT_XRGB(0, 0, 255) },
    { 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0) },
    { 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0) }
};
Trailing commas are useful whey you might add new elements to the end: you do not need to manually add commas to previous line. It makes diffs look cleanier and simplifies external code generators.
Last edited on
Got it thank youu.
Topic archived. No new replies allowed.