1

I have a structure that more or less follows this pattern:

struct sTruct {
   int count;
   struct {
       int A;
       int B;
       int C;
   } array[];   //count is the size of this array
};

I would like to be able to initialize these with something like the following syntax:

sTruct gInit1 = { 2, { {1,2,3},{4,5,6} }};

Really, that initialization syntax (or rather, the compactness of it) is more important than the specific struct layout. I do not have access to the standard containers (embedded platform), but I might be able to replicate some of their behavior if needed.

In final form, I would like to initialize an array of roughly 300 of these sTruct containers at once, just to add one more level of parenthesis.

0

1 Answer 1

7

You can't do it. If you gave the array a size you could. An alternative might be:

template < int size >
struct sTruct
{
  struct { int a, int b, int c } array[size];
};
sTruct<2> gInit1 = {{1,2,3},{4,5,6}};

But, of course, all your sTructs are different types and so it may not be what you want. Your only other alternative is going to have to be free-store based and won't give you that syntax until initialization lists in 0x.

Sign up to request clarification or add additional context in comments.

2 Comments

I suppose stuffing different sized sTructs into the same array doesn't make much sense in the first place. I may use something like this template, although I still need to add the "size" variable itself to the outer struct.
If you like this answer you may as well use boost::array. It's more or less exactly my answer but can hold anything and acts like a standard container (except it has a static size and can be initialized with aggregate syntax).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.