473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1544
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher Benson-Manica | last post by:
The following program is, to the best of my knowledge, a conforming C99 program: #include <stdio.h> typedef struct foo { int whocares; int bar; } foo;
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
2
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
2
by: DevarajA | last post by:
Can someone help me understand what flexible array members exactly are, how they behave and how could them be implemented by a i386? Also I didn't understand the two exceptions that the standards...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
20
by: mechanicfem | last post by:
I thought (as they're in c99) that flexible arrays were there for a number of reasons - but I also thought they'd be great for stepping into structures (say) that were aligned in memory after (say)...
2
by: tobiasoed | last post by:
Hello, I want to do something similar to this: #include <stdlib.h> struct coord { double val; double err; };
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.