# Constant struct arrays

This topic is 2133 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

trying to make an array of struct arrays :) Unfortunately, i am not sure if it's possible .... it should be somehow.

This is how it's usually declared:

		const D3D11_INPUT_ELEMENT_DESC ILayout0[] = // position only
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,    0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
};
const D3D11_INPUT_ELEMENT_DESC ILayout1[] = // normal
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,      0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
};


but i would like to make a one array from that:

		const D3D11_INPUT_ELEMENT_DESC LayoutArray[][] =
{
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
},
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,      0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
},
};


but unfortunately .. error ( second bracket is underlined )

error C2087: 'LayoutArray' : missing subscript
error C2078: too many initializers


Is this possible to declare it this way somehow ?

About D3D11_INPUT_ELEMENT_DESC struct. It's a DirectX11 struct and it declared

typedef struct D3D11_INPUT_ELEMENT_DESC
{
LPCSTR SemanticName;
UINT SemanticIndex;
DXGI_FORMAT Format;
UINT InputSlot;
UINT AlignedByteOffset;
D3D11_INPUT_CLASSIFICATION InputSlotClass;
UINT InstanceDataStepRate;
} 	D3D11_INPUT_ELEMENT_DESC;


##### Share on other sites
When defining a multi-dimensional array you can only leave off the first array dimension.
const D3D11_INPUT_ELEMENT_DESC LayoutArray[][5] =
{
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
},
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 0,  D3D11_INPUT_PER_VERTEX_DATA, 0},
{"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,      0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"TANGENT",  0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT,   0, 44, D3D11_INPUT_PER_VERTEX_DATA, 0},
},
};

Note that this will define a multi-dimensional array with extents [2][5]. That first array will have four extra zero-initialized entries hanging around.

##### Share on other sites

Ah, yes, it should have same amount of "items" in array.

##### Share on other sites

May I ask why you want to do this?

##### Share on other sites

By the way, if those are inside functions or methods, the construction of those arrays will be executed on each call.

Use static const, not just const.

L. Spiro

##### Share on other sites

By the way, if those are inside functions or methods, the construction of those arrays will be executed on each call.

Use static const, not just const.

L. Spiro

I'd have to look at the disassembly to check that... seems like a simple and obvious optimisation the compiler could make to me? I can believe it on some platforms though ;)

It used to be illegal to initialise non-static local arrays in C, are you thinking of that? When I learnt C the compiler we used insisted on that...

EDIT: I seem to remember you moaning about the Nintendo DS compiler in the past though being a bit crap. In general, for maximum efficiency I suppose it is a good idea to make all local const arrays (and local consts in general by extension I guess, maybe only if they are expensive to initialise), static.

EDIT2: Static locals in C++ usually have a (teeny weeny) cost as well in C++ since the compiler needs to know if they have already been constructed every time the function is called.

##### Share on other sites

It used to be illegal to initialise non-static local arrays in C, are you thinking of that? When I learnt C the compiler we used insisted on that...

No. I don’t use C that much and am not so familiar with those kinds of quirks.

EDIT2: Static locals in C++ usually have a (teeny weeny) cost as well in C++ since the compiler needs to know if they have already been constructed every time the function is called.

That much is true, but obviously less overhead than creating the whole array each time.
Where performance is concerned I make them static members of the class so they are created at start-up and create no run-time overhead.

L. Spiro

##### Share on other sites

let's say, i have lot of layouts and i thought it's a great idea to be able to access them by index. :-)

• 21
• 12
• 9
• 17
• 13