473,804 Members | 3,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4843
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
"Initializa tion Problem": <http://groups.google.com/group/comp.lang.c/
browse_frm/thread/4aef0fbeb2f5693 c/13f2da24e390e95 5?
lnk=st&q=#13f2d a24e390e955>

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
"Initializa tion 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...@ver izon.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
"Initializa tion Problem": <http://groups.google.com/group/comp.lang.c/
browse_frm/thread/4aef0fbeb2f5693 c/13f2da24e390e95 5?
lnk=st&q=#13f2d a24e390e955>

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>
>Unfortunatel y 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.cause way.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....@gma il.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....@gma il.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("ba r", "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
368
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 that contains an array of other structs. typedef struct { int nWidth; int nHeight; DWORD dwFlags; } HCA_MODE; typedef struct
5
3134
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
3647
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 wishes to avoid unnecessary garbage collection, but that is exactly what's going to happen if a lot of arrays, matrixes etc are allocated on the heap.
1
2420
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 X will either be short integer or double, and other info in the struct can be used to determine if X should be interpreted as short integer or double. I have found the docs for and for . However, one doc state that these fields only control...
8
1791
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 ByRef to the functions. An unhandled exception of type 'System.Runtime.InteropServices.MarshalDirectiveExcept ion' occurred in ConsoleApplication1.exe Additional information: PInvoke restriction: can not
3
2849
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; } *arrays; It's supposed to be a structure containing an array of chars, an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N)
11
13483
by: rayreeves | last post by:
How do I declare an array of references to the elements of an array of values? Ray
2
1916
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 file. In C, the data is in nested structs, with mixed types, floats, ints, char arrays, int arrays, variable length arrays of structs etc. My preference would be to access the data in a similar fashion to C, casting the byte array of received...
4
19335
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, "Data 1.1" },
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.