Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question About Structure Initialization For REAL Expert

Reply
Thread Tools

Question About Structure Initialization For REAL Expert

 
 
Bill Reid
Guest
Posts: n/a
 
      07-20-2008
OK, let's say that have a function menu structure declaration
like this (since I basically do):

typedef struct function_item {
char *descrip;
void(*function)(void);
} t_function_item;

typedef struct function_menu {
unsigned short num_items;
unsigned short default_item;
char *title_prompt;
t_function_item *function_items;
} t_function_menu;

Now sometimes (really just about all the time) I want to initialize it to
literal values. So this is how I do it (sort of) for a local function that
selects
a function and runs it (code that doesn't illustrate the question elided):

void my_function_1(void) {
}

void my_function_2(void) {
}

unsigned short run_function_menu(t_function_menu *function_menu) {
}

void select_function(void) {
static t_function_menu my_function_menu=
{0,0,"My Function Menu"};
static t_function_item my_function_items[]={
{"My Function 1",my_function_1},
{"My Function 2",my_function_2},
{NULL,NULL}};
my_function_menu.function_items=my_function_items;

run_function_menu(&my_function_menu);
}

My question is: for some reason if I try to initialize the menu
structure in one fell swoop, with what seems to me to be a legal
initialization, like this:

static t_function_menu my_function_menu=
{0,0,"My Function Menu",(t_function_item []){
{"My Function 1",my_function_1},
{"My Function 2",my_function_2},
{NULL,NULL}}};

I just get a bunch of syntax errors, but to me, the above seems
to be functionally identical to what I actually have to do as shown in
the select_function() example to get the thing to work...why am I
getting the errors, and IS there some way to initialize such a structure
in one initialization assignment? What am I missing here?

---
William Ernest Reid



 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      07-20-2008
Bill Reid wrote:

<snip>

>
> I just get a bunch of syntax errors, but to me, the above seems
> to be functionally identical to what I actually have to do as shown in
> the select_function() example to get the thing to work...why am I
> getting the errors, and IS there some way to initialize such a structure
> in one initialization assignment? What am I missing here?
>

What errors?

The code looks fine. Are you using a C99 compiler?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 3:43 am, Ian Collins <(E-Mail Removed)> wrote:
> Bill Reid wrote:
>
> <snip>
>
>
>
> > I just get a bunch of syntax errors, but to me, the above seems
> > to be functionally identical to what I actually have to do as shown in
> > the select_function() example to get the thing to work...why am I
> > getting the errors, and IS there some way to initialize such a structure
> > in one initialization assignment? What am I missing here?

>
> What errors?
>
> The code looks fine. Are you using a C99 compiler?

Don't feed the troll.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      07-20-2008
Ian Collins wrote:
> Bill Reid wrote:
>
> <snip>
>
>> I just get a bunch of syntax errors, but to me, the above seems
>> to be functionally identical to what I actually have to do as
>> shown in the select_function() example to get the thing to work.
>> why am I getting the errors, and IS there some way to initialize
>> such a structure in one initialization assignment? What am I
>> missing here?

>
> What errors?
>
> The code looks fine. Are you using a C99 compiler?


--
+-------------------+ .:\:\:/:/:.
| PLEASE DO NOT F :.:\:\:/:/:.:
| FEED THE TROLLS | :=.' - - '.=:
| | '=(\ 9 9 /)='
| Thank you, | ( (_) )
| Management | /`-vvv-'\
+-------------------+ / \
| | @@@ / /|,,,,,|\ \
| | @@@ /_// /^\ \\_\
@x@@x@ | | |/ WW( ( ) )WW
\||||/ | | \| __\,,\ /,,/__
\||/ | | | jgs (______Y______)
/\/\/\/\/\/\/\/\//\/\\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
================================================== ============

fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchinson


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Jul 20, 3:43 am, Ian Collins <(E-Mail Removed)> wrote:


> Don't feed the troll.


Sorry, quiet Sunday afternoon!

--
Ian Collins.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-20-2008
Joe Wright wrote:
> Ian Collins wrote:
>> (E-Mail Removed) wrote:
>>> On Jul 20, 3:43 am, Ian Collins <(E-Mail Removed)> wrote:

>>
>>> Don't feed the troll.

>>
>> Sorry, quiet Sunday afternoon!
>>

> Where are you? It's 9 PM Saturday on the US East Coast. It's 15 hours
> later in Sydney of course which would make it Sunday noon.
>

Sunny NZ.

--
Ian Collins.
 
Reply With Quote
 
rahul
Guest
Posts: n/a
 
      07-20-2008
On Jul 20, 5:27*am, "Bill Reid" <(E-Mail Removed)> wrote:

> * * static t_function_menu my_function_menu=
> * * {0,0,"My Function Menu",(t_function_item []){
> * * {"My Function 1",my_function_1},
> * * {"My Function 2",my_function_2},
> * * {NULL,NULL}}};
>

Compound literals allowed only in C99. A C99 compiler should not
complain.
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      07-20-2008
On Sun, 20 Jul 2008 12:43:23 +1200, Ian Collins wrote:
> Bill Reid wrote:


[[ context:

static t_function_menu my_function_menu=
{0,0,"My Function Menu",(t_function_item []){
{"My Function 1",my_function_1},
{"My Function 2",my_function_2},
{NULL,NULL}}};

in a function ]]

>> I just get a bunch of syntax errors, but to me, the above seems to be
>> functionally identical to what I actually have to do as shown in the
>> select_function() example to get the thing to work...why am I getting
>> the errors, and IS there some way to initialize such a structure in one
>> initialization assignment? What am I missing here?
>>

> What errors?
>
> The code looks fine. Are you using a C99 compiler?


A C99 compiler is supposed to complain for the code Bill posted. It's not
supposed to treat this as a syntax error, but it is supposed to complain
about this, and it is extremely likely to make this an error anyway. You
can't initialise a static object using a pointer to an auto object.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-20-2008
Harald van Dijk wrote:
> On Sun, 20 Jul 2008 12:43:23 +1200, Ian Collins wrote:
>> Bill Reid wrote:

>
> [[ context:
>
> static t_function_menu my_function_menu=
> {0,0,"My Function Menu",(t_function_item []){
> {"My Function 1",my_function_1},
> {"My Function 2",my_function_2},
> {NULL,NULL}}};
>
> in a function ]]
>
>>> I just get a bunch of syntax errors, but to me, the above seems to be
>>> functionally identical to what I actually have to do as shown in the
>>> select_function() example to get the thing to work...why am I getting
>>> the errors, and IS there some way to initialize such a structure in one
>>> initialization assignment? What am I missing here?
>>>

>> What errors?
>>
>> The code looks fine. Are you using a C99 compiler?

>
> A C99 compiler is supposed to complain for the code Bill posted. It's not
> supposed to treat this as a syntax error, but it is supposed to complain
> about this, and it is extremely likely to make this an error anyway. You
> can't initialise a static object using a pointer to an auto object.


Which auto object?

--
Ian Collins.
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      07-20-2008
On Sun, 20 Jul 2008 20:38:49 +1200, Ian Collins wrote:
> Harald van Dijk wrote:
>> On Sun, 20 Jul 2008 12:43:23 +1200, Ian Collins wrote:
>>> Bill Reid wrote:

>>
>> [[ context:
>>
>> static t_function_menu my_function_menu=
>> {0,0,"My Function Menu",(t_function_item []){
>> {"My Function 1",my_function_1},
>> {"My Function 2",my_function_2},
>> {NULL,NULL}}};
>>
>> in a function ]]
>>
>>> The code looks fine. Are you using a C99 compiler?

>>
>> You can't initialise a static object using a pointer to an auto
>> object.

>
> Which auto object?


(t_function_item []){
{"My Function 1",my_function_1},
{"My Function 2",my_function_2},
{NULL,NULL}}

A compound literal in a function definition has automatic storage
duration. There is no exception for initialisation.
 
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
Question for a REAL expert on casting double to float... Bill Reid C Programming 23 07-15-2008 08:35 AM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM
Anyone here is a real JS expert? Wow Javascript 5 07-26-2004 05:46 AM



Advertisments