469,610 Members | 1,844 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

flexible array in a struct, or something similar

In C++ I want to statically create an array that contains variable arrays
inside of it. Or something similar.

Essentially I want to do something like this:
{
{"name1", {a, b, c} },
{"name2", {b, c} },
{"name2", {d} }
}
I want an array of names, and with each name I want to associate an list of
characteristics., of unknown size. But this will all be known at compile
time, so it is a static list.

Or the equivalent. It seems to be that this should be possible with the
compiler.
Worst case scenario I can always do:
int *foo[] = {a, b, c};
{"name1", foo}, etc...

But that seems like too much work on my part. The compiler should be able to
do this work.
If I do the obvious and declare flexible arrays I get a warning about a
nonstandard extension. Then an error "arrays of objects containing zero-size
arrays are illegal"

Thanks
Sep 20 '06 #1
3 1403
In C++ I want to statically create an array that contains variable arrays
inside of it. Or something similar.

Essentially I want to do something like this:
{
{"name1", {a, b, c} },
{"name2", {b, c} },
{"name2", {d} }
}
I want an array of names, and with each name I want to associate an list
of
characteristics., of unknown size. But this will all be known at compile
time, so it is a static list.
A following will work. I advice use not this solution, however.

struct Record
{
static const int max_values = 10;

char *name;
int values[max_values];
};

int main(int argc, char* argv[])
{
Record records[] =
{
{"name1", {1, 2, 3} },
{"name2", {2, 3} },
{"name2", {4} }
};

return 0;
}

P.S. Next version of C++ will allow to define such consturcts much more
efficiently. See std::
initializer_list for details.

--
Vladimir Nesterovsky
Sep 21 '06 #2
This is very similar to the approach I am using.

But I am curious, why do you advise to not use this solution? Other than it
will waste a few hundred bytes of memeory at most. Since there is no viable
alternative solution, I am curious why this one is bad?

Thanks

"Vladimir Nesterovsky" wrote:
In C++ I want to statically create an array that contains variable arrays
inside of it. Or something similar.

Essentially I want to do something like this:
{
{"name1", {a, b, c} },
{"name2", {b, c} },
{"name2", {d} }
}
I want an array of names, and with each name I want to associate an list
of
characteristics., of unknown size. But this will all be known at compile
time, so it is a static list.

A following will work. I advice use not this solution, however.

struct Record
{
static const int max_values = 10;

char *name;
int values[max_values];
};

int main(int argc, char* argv[])
{
Record records[] =
{
{"name1", {1, 2, 3} },
{"name2", {2, 3} },
{"name2", {4} }
};

return 0;
}

P.S. Next version of C++ will allow to define such consturcts much more
efficiently. See std::
initializer_list for details.

--
Vladimir Nesterovsky
Sep 21 '06 #3
This is very similar to the approach I am using.
>
But I am curious, why do you advise to not use this solution? Other than
it
will waste a few hundred bytes of memeory at most. Since there is no
viable
alternative solution, I am curious why this one is bad?
It's matter of taste. Not always syntactic sugar justifies itself.
E.g.: RAII and smart pointers are good, iostreams are bad.

--
Vladimir Nesterovsky
Sep 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Christopher Benson-Manica | last post: by
10 posts views Thread by Adam Warner | last post: by
5 posts views Thread by Stijn van Dongen | last post: by
2 posts views Thread by Christopher Benson-Manica | last post: by
2 posts views Thread by DevarajA | last post: by
20 posts views Thread by mechanicfem | last post: by
2 posts views Thread by tobiasoed | last post: by
3 posts views Thread by Hallvard B Furuseth | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.