473,503 Members | 7,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5979


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 (40°39.22'N, 111°50.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1340
by: Koster | last post by:
Just a quickie: I heard that arrays declared in the global scope are automatically initialised with a block of zeros. To help with making my C source compatible with multiple compilers, I'd like...
7
2668
by: Chandrashekar Tippur | last post by:
All, I need to initialize an array of structures but I don't know how may elements are there. I tried to malloc the array but I am not sure how to initialize them. Snippet: struct myarray{...
2
2111
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
5
3111
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;
15
3814
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
11
2605
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
5
2277
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ...
13
2306
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
2
11931
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and...
0
7207
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,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7291
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,...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3180
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...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
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 ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.