473,396 Members | 1,933 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,396 software developers and data experts.

Declaring structs with anonymous arrays

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?
Sep 30 '08 #1
9 4813
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.

--
Er*********@sun.com
Sep 30 '08 #2


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.
Sep 30 '08 #3
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.
Sep 30 '08 #4


jameskuy...@verizon.net 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.
Sep 30 '08 #5
andreyvul wrote:
>
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?
Not that I know of. (Unless you happen to know that the arrays
are always three elements long and you are willing to use a slightly
different data structure, but I'm assuming that's not the case.)
Unfortunately VC isn't as standards-
conformant as gcc,
Yikes!
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.
I can't get the preprocessor to write code I don't know how to
write myself. Since I can't think of any C90 way to do the job while
keeping the arrays anonymous (but why do you care?), I can't think of
a way the preprocessor could somehow rescue the situation.

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).

--
Er*********@sun.com
Sep 30 '08 #6
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.

Sep 30 '08 #7
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 sm**@spam.causeway.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Sep 30 '08 #8
On Sep 30, 7:45 pm, andreyvul <andrey....@gmail.comwrote:
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") } },
{ /* ... */
};
Sep 30 '08 #9


vipps...@gmail.com wrote:
On Sep 30, 7:45 pm, andreyvul <andrey....@gmail.comwrote:
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.
Sep 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
1
by: Fuzzy via .NET 247 | last post by:
I have a struct that I would like to control the field layout on in the same manner of UNION structs in C++. This is not for passing data to unmanaged code, it is to save memory space because field...
8
by: Bryan G | last post by:
Hi, I'm working on a VB project which involves using C library functions which take struct pointers as args, and I keep running into this error when trying to pass either an IntPtr or a Structure...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
11
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
2
by: =?Utf-8?B?U2V0aEluTUk=?= | last post by:
I am a total newb at .net, and I have not been able to search out a best practice answer to what must be a common problem. My app must process binary data from a UDP socket, a MSMQ queue and a...
4
by: KioKrofov | last post by:
Currently I have (a much bigger version of) the code below in my main cpp file: struct myData { int dataCodeNum; char* dataName; } myData arrayOfData1={ { 1, ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.