Question About Structure Initialization For REAL Expert

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Reid

    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(v oid) {
    }

    void my_function_2(v oid) {
    }

    unsigned short run_function_me nu(t_function_m enu *function_menu) {
    }

    void select_function (void) {
    static t_function_menu my_function_men u=
    {0,0,"My Function Menu"};
    static t_function_item my_function_ite ms[]={
    {"My Function 1",my_function_ 1},
    {"My Function 2",my_function_ 2},
    {NULL,NULL}};
    my_function_men u.function_item s=my_function_i tems;

    run_function_me nu(&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_men u=
    {0,0,"My Function Menu",(t_functi on_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



  • Ian Collins

    #2
    Re: Question About Structure Initialization For REAL Expert

    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.

    Comment

    • vippstar@gmail.com

      #3
      Re: Question About Structure Initialization For REAL Expert

      On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.co mwrote:
      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.

      Comment

      • Ian Collins

        #4
        Re: Question About Structure Initialization For REAL Expert

        vippstar@gmail. com wrote:
        On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.co mwrote:
        Don't feed the troll.
        Sorry, quiet Sunday afternoon!

        --
        Ian Collins.

        Comment

        • CBFalconer

          #5
          Re: Question About Structure Initialization For REAL Expert

          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


          Comment

          • Joe Wright

            #6
            Re: Question About Structure Initialization For REAL Expert

            Ian Collins wrote:
            vippstar@gmail. com wrote:
            >On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.co mwrote:
            >
            >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 ---

            Comment

            • Ian Collins

              #7
              Re: Question About Structure Initialization For REAL Expert

              Joe Wright wrote:
              Ian Collins wrote:
              >vippstar@gmail. com wrote:
              >>On Jul 20, 3:43 am, Ian Collins <ian-n...@hotmail.co mwrote:
              >>
              >>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.

              Comment

              • rahul

                #8
                Re: Question About Structure Initialization For REAL Expert

                On Jul 20, 5:27 am, "Bill Reid" <hormelf...@hap pyhealthy.netwr ote:
                    static t_function_menu my_function_men u=
                    {0,0,"My Function Menu",(t_functi on_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.

                Comment

                • Harald van =?UTF-8?b?RMSzaw==?=

                  #9
                  Re: Question About Structure Initialization For REAL Expert

                  On Sun, 20 Jul 2008 12:43:23 +1200, Ian Collins wrote:
                  Bill Reid wrote:
                  [[ context:

                  static t_function_menu my_function_men u=
                  {0,0,"My Function Menu",(t_functi on_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_functio n() 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
                  >initializati on 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.

                  Comment

                  • Ian Collins

                    #10
                    Re: Question About Structure Initialization For REAL Expert

                    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_men u=
                    {0,0,"My Function Menu",(t_functi on_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
                    >>functionall y identical to what I actually have to do as shown in the
                    >>select_functi on() 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
                    >>initializatio n 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.

                    Comment

                    • Harald van =?UTF-8?b?RMSzaw==?=

                      #11
                      Re: Question About Structure Initialization For REAL Expert

                      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_men u=
                      > {0,0,"My Function Menu",(t_functi on_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_ite m []){
                      {"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.

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: Question About Structure Initialization For REAL Expert

                        rahul <rahulsinner@gm ail.comwrites:
                        On Jul 20, 5:27 am, "Bill Reid" <hormelf...@hap pyhealthy.netwr ote:
                        >
                        >    static t_function_menu my_function_men u=
                        >    {0,0,"My Function Menu",(t_functi on_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.

                        Comment

                        • Ben Bacarisse

                          #13
                          Re: Question About Structure Initialization For REAL Expert

                          Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                          "Bill Reid" <hormelfree@hap pyhealthy.netwr ites:
                          <snip>
                          > static t_function_menu my_function_men u=
                          > {0,0,"My Function Menu",(t_functi on_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.

                          Comment

                          • Ben Bacarisse

                            #14
                            Re: Question About Structure Initialization For REAL Expert

                            "Bill Reid" <hormelfree@hap pyhealthy.netwr ites:
                            <snip>
                            Ben Bacarisse <ben.usenet@bsb .me.ukwrote in message
                            news:871w1o7u28 .fsf@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_men u = {
                            > 2, 0, "My Function Menu",
                            > (t_function_ite m[]){
                            > {"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_me nu(&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(v oid)
                            {
                            printf("in %s()\n", __func__);
                            }

                            void my_function_2(v oid)
                            {
                            printf("in %s()\n", __func__);
                            }

                            void run_function_me nu(t_function_m enu *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_ite ms[item].descrip);
                            function_menu->function_ite ms[item].function();
                            }

                            int main(int argc, char **argv)
                            {
                            t_function_menu my_function_men u = {
                            2, 0, "My Function Menu",
                            (t_function_ite m[]){
                            {"My Function 1", my_function_1},
                            {"My Function 2", my_function_2},
                            {NULL, NULL}
                            }
                            };
                            run_function_me nu(&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.

                            Comment

                            • Ben Bacarisse

                              #15
                              Re: Question About Structure Initialization For REAL Expert

                              "Bill Reid" <hormelfree@hap pyhealthy.netwr ites:
                              Ben Bacarisse <ben.usenet@bsb .me.ukwrote in message
                              news:87abgc4kyj .fsf@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_ite ms[] = {
                              {"My Function 1", my_function_1},
                              {"My Function 2", my_function_2},
                              {NULL, NULL}};
                              t_function_menu my_function_men u = {
                              (sizeof my_function_ite ms/sizeof my_function_ite ms[0])-1, 0,
                              "My Function Menu",my_functi on_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_ite ms as
                              static:
                              >
                              static t_function_item my_function_ite ms[] = {
                              {"My Function 1", my_function_1},
                              {"My Function 2", my_function_2},
                              {NULL, NULL}};
                              t_function_menu my_function_men u = {
                              (sizeof my_function_ite ms/sizeof my_function_ite ms[0])-1, 0,
                              "My Function Menu",my_functi on_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.

                              Comment

                              Working...