473,395 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Jul 20 '08 #1
14 1694
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.
Jul 20 '08 #2
On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote:
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.
Jul 20 '08 #3
vi******@gmail.com wrote:
On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote:
Don't feed the troll.
Sorry, quiet Sunday afternoon!

--
Ian Collins.
Jul 20 '08 #4
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
Jul 20 '08 #5
Ian Collins wrote:
vi******@gmail.com wrote:
>On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote:
>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 ---
Jul 20 '08 #6
Joe Wright wrote:
Ian Collins wrote:
>vi******@gmail.com wrote:
>>On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.comwrote:
>>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.
Jul 20 '08 #7
On Jul 20, 5:27*am, "Bill Reid" <hormelf...@happyhealthy.netwrote:
* * 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.
Jul 20 '08 #8
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.
Jul 20 '08 #9
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.
Jul 20 '08 #10
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.
Jul 20 '08 #11
rahul <ra*********@gmail.comwrites:
On Jul 20, 5:27Â*am, "Bill Reid" <hormelf...@happyhealthy.netwrote:
>Â* Â* 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.
Jul 20 '08 #12
Ben Bacarisse <be********@bsb.me.ukwrites:
"Bill Reid" <ho********@happyhealthy.netwrites:
<snip>
> 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.
Jul 20 '08 #13
"Bill Reid" <ho********@happyhealthy.netwrites:
<snip>
Ben Bacarisse <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
<snip>
>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.
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.
Jul 20 '08 #14
"Bill Reid" <ho********@happyhealthy.netwrites:
Ben Bacarisse <be********@bsb.me.ukwrote in message
news:87************@bsb.me.uk...
<snip>
>Hmm... are you using lcc-win32? This program:
<snip>
>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>
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.
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.
Jul 20 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
18
by: Minti | last post by:
I was reading some text and I came across the following snippet switch('5') { int x = 123; case '5': printf("The value of x %d\n", x); break; }
9
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1;...
3
by: | last post by:
Hi! How to extand and access the internat Office object structure. Is there any Dev Kit for Microsoft Partners that in not available to "normal" users? Something like NOKIA back entrence!? ...
3
by: JV | last post by:
I'm trying to understand how ASP.NET creates web folders when it creates a new ASP.NET project. I am experienced with DotNet development, but not an expert on how IIS works. Or, this may be a...
5
by: Rich | last post by:
Suppose the following: typedef void (*funcptrs)(void); typedef struct { unsigned int *in; unsigned int *out; unsigned int *overrun; funcptrs myptrs; /* >=1 for ansi */ } yada;
14
by: Sumit77Sen | last post by:
who calls main() ?
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
17
by: jb.simon | last post by:
Recently I was pinged in a code review about my use of the initialization method AStruct myStruct = { 0 } ; Which initializes all elements of the myStruct to 0. I was questioned on it because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.