Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Declaring structs with anonymous arrays

Reply
Thread Tools

Declaring structs with anonymous arrays

 
 
andreyvul
Guest
Posts: n/a
 
      09-30-2008
I'm trying to do the following:

typedef struct {
char *bar;
char **baz;
} foo;

const foo fop[] = {
{ "foo", { "bar", "baz", "bax" } },
{ "goo", { "car", "cdr", "cfr" } }
};

Now, MSVC 8 (VC++ 2005) states compilation failure due to the '{'
before the "bar" and the '}' following the "bar",
and the same situation for "car" and "cfr".
Any suggestions?
 
Reply With Quote
 
 
 
 
andreyvul
Guest
Posts: n/a
 
      09-30-2008


Eric Sosman wrote:
> andreyvul wrote:
> > I'm trying to do the following:
> >
> > typedef struct {
> > char *bar;
> > char **baz;
> > } foo;
> >
> > const foo fop[] = {
> > { "foo", { "bar", "baz", "bax" } },
> > { "goo", { "car", "cdr", "cfr" } }
> > };
> >
> > Now, MSVC 8 (VC++ 2005) states compilation failure due to the '{'
> > before the "bar" and the '}' following the "bar",
> > and the same situation for "car" and "cfr".
> > Any suggestions?

>
> If you're willing to waive anonymity:
>
> static char *fop0[] = { "bar", "baz", "bax", };
> static char *fop1[] = { "car", "cdr", "cfr", };
> const foo fop[] = {
> { "foo", fop0 },
> { "goo", fop1 },
> };
>
> Add qualifiers as desired.
>

Is there a way for anonymity to remain, i.e. similar to my example,
without violating ANSI C 90/99? Unfortunately VC isn't as standards-
conformant as gcc, so if hacks are needed, give an example
(preprocessor only, please) such as how to use the preprocessor as a
compiler for ENTRY{str, array} entries.
 
Reply With Quote
 
 
 
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      09-30-2008
andreyvul wrote:
> I'm trying to do the following:
>
> typedef struct {
> char *bar;
> char **baz;
> } foo;
>
> const foo fop[] = {
> { "foo", { "bar", "baz", "bax" } },
> { "goo", { "car", "cdr", "cfr" } }
> };
>
> Now, MSVC 8 (VC++ 2005) states compilation failure due to the '{'
> before the "bar" and the '}' following the "bar",
> and the same situation for "car" and "cfr".
> Any suggestions?


Look at the thread from 2008-09-22 in this newsgroup titled
"Initialization Problem": <http://groups.google.com/group/comp.lang.c/
browse_frm/thread/4aef0fbeb2f5693c/13f2da24e390e955?
lnk=st&q=#13f2da24e390e955>

If you can restrict the portability of your code to compilers which
support the C99 feature of compound literals, then you can write:

const foo fop = {
{ "foo", (char *[]){"bar", "baz", "bax"}},
....

The key problem is that you have to allocate space for foo[i].baz,
foo[i].baz[j], and foo[i].baz[j][k]. Your code doesn't allocate any
memory for foo[i].baz[j]. Compound literals allocate space with static
storage duration for the arrays that they point to, just like string
literals do.

Notice that this is much simpler than the corresponding code in the
"Initialization Problem" thread, because you're using char where
Thorsten Schilling was using int. As a result, you can use string
literals for much of the work. His version had to use compound
literals everywhere.
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      09-30-2008


(E-Mail Removed) wrote:
> andreyvul wrote:
> > I'm trying to do the following:
> >
> > typedef struct {
> > char *bar;
> > char **baz;
> > } foo;
> >
> > const foo fop[] = {
> > { "foo", { "bar", "baz", "bax" } },
> > { "goo", { "car", "cdr", "cfr" } }
> > };
> >
> > Now, MSVC 8 (VC++ 2005) states compilation failure due to the '{'
> > before the "bar" and the '}' following the "bar",
> > and the same situation for "car" and "cfr".
> > Any suggestions?

>
> Look at the thread from 2008-09-22 in this newsgroup titled
> "Initialization Problem": <http://groups.google.com/group/comp.lang.c/
> browse_frm/thread/4aef0fbeb2f5693c/13f2da24e390e955?
> lnk=st&q=#13f2da24e390e955>
>
> If you can restrict the portability of your code to compilers which
> support the C99 feature of compound literals, then you can write:
>
> const foo fop = {
> { "foo", (char *[]){"bar", "baz", "bax"}},
> ...
>
> The key problem is that you have to allocate space for foo[i].baz,
> foo[i].baz[j], and foo[i].baz[j][k]. Your code doesn't allocate any
> memory for foo[i].baz[j]. Compound literals allocate space with static
> storage duration for the arrays that they point to, just like string
> literals do.

Well then, I'm switching to MinGW from VC then.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-30-2008
andreyvul wrote:
> I'm trying to do the following:



typedef struct
{
char *bar;
char *baz[3]; /* was char **baz */
} foo;

const foo fop[] = {
{"foo", {"bar", "baz", "bax"}},
{"goo", {"car", "cdr", "cfr"}}
};



> Now, MSVC 8 (VC++ 2005) states compilation failure due to the '{'
> before the "bar" and the '}' following the "bar",
> and the same situation for "car" and "cfr".
> Any suggestions?


The double indirection '**baz' is the problem. If you want that form,
you will need to allocate space for the pointer array, which will make
the 'const' suspect and filling the structure by an initializer problematic.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      09-30-2008
Eric Sosman wrote, On 30/09/08 18:43:
> andreyvul wrote:


<snip>

>> Is there a way for anonymity to remain, i.e. similar to my example,
>> without violating ANSI C 90/99?


<snip>

>> Unfortunately VC isn't as standards-
>> conformant as gcc,

>
> Yikes!


No need for the "Yikes!". MSVC actually conforms very well to C90 when
properly prompted (probably about as well as gcc when gcc is properly
prompted).

<snip>

> James Kuyper suggests using compound literals, which seems a
> plausible approach. But they're a C99 feature, and from all I've
> read Microsoft have not been prompt to introduce C99 support (but
> my information may be wrong; check for yourself).


I believe you are correct and MS have done negligible work towards C99.
--
Flash Gordon
If spamming me sent it to http://www.velocityreviews.com/forums/(E-Mail Removed)
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      09-30-2008
On Sep 30, 7:45 pm, andreyvul <(E-Mail Removed)> wrote:
> I'm trying to do the following:
>
> typedef struct {
> char *bar;
> char **baz;
>
> } foo;
>
> const foo fop[] = {
> { "foo", { "bar", "baz", "bax" } },
> { "goo", { "car", "cdr", "cfr" } }
>
> };
>
> Now, MSVC 8 (VC++ 2005) states compilation failure due to the '{'
> before the "bar" and the '}' following the "bar",
> and the same situation for "car" and "cfr".
> Any suggestions?




C99 solution:

const foo fop[] = {
{ "foo", (char *[]){ "bar", "baz", "bax"} },
{ "goo", (char *[]){ "car", "cdr", "cfr" } }
};

However, you can't modify the strings baz points to, because they're
string literals.
To fix this...

#define NOTLITERAL(x) (char []){x}

const foo fop[] = {
{ "foo", (char *[]){ NOTLITERAL("bar"), NOTLITERAL("baz"),
NOTLITERAL("bax") } },
{ /* ... */
};
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      09-30-2008


(E-Mail Removed) wrote:
> On Sep 30, 7:45 pm, andreyvul <(E-Mail Removed)> wrote:
> > I'm trying to do the following:
> >
> > typedef struct {
> > char *bar;
> > char **baz;
> >
> > } foo;
> >
> > const foo fop[] = {
> > { "foo", { "bar", "baz", "bax" } },
> > { "goo", { "car", "cdr", "cfr" } }
> >
> > };
> >
> > Now, MSVC 8 (VC++ 2005) states compilation failure due to the '{'
> > before the "bar" and the '}' following the "bar",
> > and the same situation for "car" and "cfr".
> > Any suggestions?

>
>
>
> C99 solution:
>
> const foo fop[] = {
> { "foo", (char *[]){ "bar", "baz", "bax"} },
> { "goo", (char *[]){ "car", "cdr", "cfr" } }
> };

Which can be converted into a macro: #define stringarray(...) (const
char *[]){ __VA_ARGS__, NULL}
Then replace (char *[]){ "bar", "baz", "bax", NULL} with
stringarray("bar", "baz", "bax")
Note: I reuse the NULL functionality of char* strings in char**
arrays.
>
> However, you can't modify the strings baz points to, because they're
> string literals.

Not necessary. My code requires use of const char ** and const char*.
The "de-constification" is unneeded.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring structs vs. classes Markus Svilans C++ 7 05-16-2006 01:15 PM
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments