468,727 Members | 1,478 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Initializing an Array of Pointers to Structs

I am trying to initialize an arrary of
pointers to structs with constants.

Sample code:

struct mystruct {
char *text;
int number;
};

struct mystruct *array[] = {
{"string1", 1},
{"string2", 2},
{"string3", 3},
};

GCC complains: "braces around scaler initializer"

I would think {"string1", 1} would be
replaced with an address upon compilation.

For example,
char *strings[] = {"string1", "string2", NULL};
works.

Any help would be appreciated.

Chad

Nov 14 '05 #1
4 4949


cc*****@yahoo.com wrote:
I am trying to initialize an arrary of
pointers to structs with constants.

Sample code:

struct mystruct {
char *text;
int number;
};

struct mystruct *array[] = {
{"string1", 1},
{"string2", 2},
{"string3", 3},
};

GCC complains: "braces around scaler initializer"

I would think {"string1", 1} would be
replaced with an address upon compilation.

For example,
char *strings[] = {"string1", "string2", NULL};
works.

Any help would be appreciated.


struct mystruct array_data[] = {
{ "string1", 1 },
{ "string2", 2 },
{ "string3", 3 },
};

struct mystruct *array[] = {
&array_data[0], /* or `array_data + 0' */
&array_data[1],
&array_data[2],
};

--
Er*********@sun.com

Nov 14 '05 #2
Is it possible to initialize the array without declaring
an intermediate variable to get an address?

Chad

Nov 14 '05 #3


cc*****@yahoo.com wrote:
Is it possible to initialize the array without declaring
an intermediate variable to get an address?


(See up-thread for snipped context: He wants to initialize
an array of pointers to structs that are also initialized.)

No. The string literal is the only C construct that
creates and initializes an "anonymous" object. All other
initialized objects must have names.

--
Er*********@sun.com

Nov 14 '05 #4
>cc*****@yahoo.com wrote:
Is it possible to initialize the array without declaring
an intermediate variable to get an address?

In article <d3**********@news1brm.Central.Sun.COM>
Eric Sosman <er*********@sun.com> wrote: (See up-thread for snipped context: He wants to initialize
an array of pointers to structs that are also initialized.)

No. The string literal is the only C construct that
creates and initializes an "anonymous" object. All other
initialized objects must have names.


Except in C99, where compound literals create anonymous
objects.

For instance, in C99 (but not C89), one can write:

/* tables for decoding SCSI <code,subcode> pairs */

struct subcode {
int sc_num;
char *sc_msg;
};
struct code {
int co_num;
size_t co_nsub;
struct subcode *co_subc;
};

struct code scsi_codetab[] = {
{
0, 2,
(struct subcode [2]) {
{ 0, "0/0" },
{ 1, "0/1" },
},
},
{
1, 3,
(struct subcode [3]) {
{ 0, "1/0" },
{ 27, "1/27" },
{ 40, "1/40" },
}
},
};

(The above is not intended to be particularly useful; in particular
the code and subcode numbers are made up and the sc_msg strings are
ridiculous. But SCSI does have code/subcode pairs and the overall
technique itself is reasonable.)

Note that even though you *can* do this in C99, it is probably
better to use C89 constructs to match up the "number of subcodes"
initializer (for co_nsub) and actual table sizes automatically.
That is, writing this out "longhand" is actually less fragile:

struct subcode {
int sc_num;
char *sc_msg;
};
struct code {
int co_num;
size_t co_nsub;
struct subcode *co_subc;
};

static struct subcode sc0_table[] = {
{ 0, "0/0" },
{ 1, "0/1" },
};
static struct subcode sc1_table[] = {
{ 0, "1/0" },
{ 27, "1/27" },
{ 40, "1/40" },
};
#define COUNT_AND_TABLE(x) sizeof x / sizeof x[0], x
struct code scsi_codetab[] = {
{ 0, COUNT_AND_TABLE(sc0_table) },
{ 1, COUNT_AND_TABLE(sc1_table) },
};

There is in fact a more general rule of programming that applies
here: "if something is complex, give it a name."
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Koster | last post: by
7 posts views Thread by Chandrashekar Tippur | last post: by
2 posts views Thread by Simon Morgan | last post: by
15 posts views Thread by Paminu | last post: by
5 posts views Thread by dev_15 | last post: by
13 posts views Thread by WaterWalk | last post: by
1 post views Thread by CARIGAR | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.