Question About Structure Initialization For REAL Expert | | |
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 | | | | re: Question About Structure Initialization For REAL Expert
Bill Reid wrote:
<snip> Quote:
>
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. | | | | re: Question About Structure Initialization For REAL Expert
On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote: Quote:
Bill Reid wrote:
>
<snip>
>
>
> Quote:
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. | | | | re: Question About Structure Initialization For REAL Expert vippstar@gmail.com wrote: Quote:
On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote:
Quote:
Don't feed the troll.
Sorry, quiet Sunday afternoon!
--
Ian Collins. | | | | re: Question About Structure Initialization For REAL Expert
Ian Collins wrote: Quote:
Bill Reid wrote:
>
<snip>
> Quote:
>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 | | | | re: Question About Structure Initialization For REAL Expert
Ian Collins wrote: Quote: vippstar@gmail.com wrote: Quote:
>On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote:
> Quote:
>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.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- | | | | re: Question About Structure Initialization For REAL Expert
Joe Wright wrote: Quote:
Ian Collins wrote: Quote:
> vippstar@gmail.com wrote: Quote:
>>On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote:
>> Quote:
>>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. | | | | re: Question About Structure Initialization For REAL Expert
On Jul 20, 5:27*am, "Bill Reid" <hormelf...@happyhealthy.netwrote: Quote:
* * 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. | | | | re: Question About Structure Initialization For REAL Expert
On Sun, 20 Jul 2008 12:43:23 +1200, Ian Collins 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 ]] Quote: Quote:
>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. | | | | re: Question About Structure Initialization For REAL Expert
Harald van Dijk wrote: Quote:
On Sun, 20 Jul 2008 12:43:23 +1200, Ian Collins 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 ]]
> Quote: Quote:
>>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. | | | | re: Question About Structure Initialization For REAL Expert
On Sun, 20 Jul 2008 20:38:49 +1200, Ian Collins wrote: Quote:
Harald van Dijk wrote: Quote:
>On Sun, 20 Jul 2008 12:43:23 +1200, Ian Collins wrote: Quote:
>>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 ]]
>> Quote:
>>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. | | | | re: Question About Structure Initialization For REAL Expert
rahul <rahulsinner@gmail.comwrites: Quote:
On Jul 20, 5:27Â*am, "Bill Reid" <hormelf...@happyhealthy.netwrote:
> Quote:
>Â* Â* 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.
Yes it should (at last I think it should) -- the object is static.
You can just make the whole menu automatic (see my reply).
--
Ben. | | | | re: Question About Structure Initialization For REAL Expert
Ben Bacarisse <ben.usenet@bsb.me.ukwrites: Quote:
"Bill Reid" <hormelfree@happyhealthy.netwrites:
<snip> Quote: Quote:
> 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,
>
The errors are due to the fact that you can't initialise a pointer
just by starting a new set of {}s.
I missed the () introducing a compound literal. Curiously the rest of
my answer is correct and helpful!
The errors will either be (a) not using a C99 compiler or (b) the
correct complaint that a static is being initialised with a compound
literal.
--
Ben. | | | | re: Question About Structure Initialization For REAL Expert
"Bill Reid" <hormelfree@happyhealthy.netwrites:
<snip> Quote:
Ben Bacarisse <ben.usenet@bsb.me.ukwrote in message
news:871w1o7u28.fsf@bsb.me.uk...
<snip> Quote: Quote:
>or (2) use C99 compound literals and make the menu automatic rather
>than static like this:
>>
> t_function_menu my_function_menu = {
> 2, 0, "My Function Menu",
> (t_function_item[]){
> {"My Function 1", my_function_1},
> {"My Function 2", my_function_2},
> {NULL, NULL}
> }
> };
>
Yeah, I can't do that with this compiler, but is there some difference
in C99 related to automatic initializations versus static? I'm using static
because if don't, when I do this call after the initialization:
>
run_function_menu(&my_function_menu);
>
I get a whole bunch of nothing
Hmm... are you using lcc-win32? This program:
#include <stdio.h>
#include <stdlib.h>
typedef struct function_item {
const char *descrip;
void(*function)(void);
} t_function_item;
typedef struct function_menu {
unsigned short num_items;
unsigned short default_item;
const char *title_prompt;
t_function_item *function_items;
} t_function_menu;
void my_function_1(void)
{
printf("in %s()\n", __func__);
}
void my_function_2(void)
{
printf("in %s()\n", __func__);
}
void run_function_menu(t_function_menu *function_menu, int item)
{
if (item < 0 || item >= function_menu->num_items)
item = function_menu->default_item;
printf("Do #%d %s: ", item, function_menu->function_items[item].descrip);
function_menu->function_items[item].function();
}
int main(int argc, char **argv)
{
t_function_menu my_function_menu = {
2, 0, "My Function Menu",
(t_function_item[]){
{"My Function 1", my_function_1},
{"My Function 2", my_function_2},
{NULL, NULL}
}
};
run_function_menu(&my_function_menu, argc 1 ? atoi(argv[1]) : 0);
return 0;
}
runs fine using gcc but gives a segmentation fault using lcc-win32. Quote:
Yeah, for a local initialization I guess I can't do that with this
compiler...
It looks like a general bug in the way structs get initialised to me.
Take this, for example:
#include <stdio.h>
int main(void)
{
int array[] = { 1, 2, 3 };
struct s {
int n;
int *items;
} s = { 3, array };
printf("%p %p\n", (void *)array, (void *)s.items);
return 0;
}
s.items does not get initialised.
--
Ben. | | | | re: Question About Structure Initialization For REAL Expert
"Bill Reid" <hormelfree@happyhealthy.netwrites: Quote:
Ben Bacarisse <ben.usenet@bsb.me.ukwrote in message
news:87abgc4kyj.fsf@bsb.me.uk...
<snip> Quote: Quote:
>Hmm... are you using lcc-win32? This program:
<snip> Quote: Quote:
>runs fine using gcc but gives a segmentation fault using lcc-win32.
>
Well, no, that's not the compiler I'm using, and per my original
question (now answered) I couldn't do the initialization in the first
place without getting syntax errors.
It was just a guess.
<snip> Quote:
But this is "illegal initialization":
>
t_function_item my_function_items[] = {
{"My Function 1", my_function_1},
{"My Function 2", my_function_2},
{NULL, NULL}};
t_function_menu my_function_menu = {
(sizeof my_function_items/sizeof my_function_items[0])-1, 0,
"My Function Menu",my_function_items};
Yes, in C90 all initialisers must be "compile-time constants" so a
pointer to an automatic variable is not allowed. Quote:
But can be fixed by declaring only my_function_items as
static:
>
static t_function_item my_function_items[] = {
{"My Function 1", my_function_1},
{"My Function 2", my_function_2},
{NULL, NULL}};
t_function_menu my_function_menu = {
(sizeof my_function_items/sizeof my_function_items[0])-1, 0,
"My Function Menu",my_function_items};
>
So is this all completely correct behavior under "C89", which
is apparently the controlling "standard" for this type of stuff for
me?
Yes, that is all standard and correct. If you are tied to C90 (=C89)
then it also explains your initial syntax errors. You originally
posted code that contained a compound literal (a C99 feature) and your
compiler would very likely reject it. That's why I went off on the
C99 tangent -- I thought is was available to you.
--
Ben. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,439 network members.
|