Initializer element Error

Discussion in 'C Programming' started by vib, May 18, 2006.

  1. vib

    vib Guest

    Hi there,

    I wish to get some advice on the problem I face in initializing a
    table.

    Here is the code. I've an array of strings, ptrNameString . I want to
    initialize a table, mySoftKeyTable with some contents of the string
    from the array. This initialization is not in any function, but rather
    outside of any function. However, the compiler complaints, the related
    lines( L1, L2, L3, L4) of "Initializer element is not constant". I
    guess, it expects a constant but getting a non-constant instead.

    Please advise.

    Thanks in advance.
    vibnwis

    char * ptrNameString [5] = { "One", "Two", "Three", "Four", "Five" };

    WWM_SOFTKEY_ITEM_DATA mySoftKeyTable[] =
    /* L1 */ {{
    /*adrStrings[STR_SETUP].textStr[ENGLISH]*/*(ptrNameString),
    (void (* ) (void *)) winHelloCreate, UGL_NULL, 0},
    /* L2 */ {
    /*adrStrings[STR_DIAL].textStr[ENGLISH]*/*(ptrNameString+1), (void (* )

    (void *)) winHelloCreateLine2, UGL_NULL, 1 },
    /* L3 */ {
    /*adrStrings[STR_REDIAL].textStr[ENGLISH]*/*(ptrNameString+2), (void (*
    )
    (void *)) winHelloCreateLine3, UGL_NULL, 2 },
    /* L4 */ {
    /*adrStrings[STR_EXIT].textStr[ENGLISH]*/*(ptrNameString+3),
    (void (* ) (void *)) winHelloCreateLine4, UGL_NULL, 3 },
    { UGL_NULL, UGL_NULL, UGL_NULL, UGL_NULL} };

    Note: winHelloCreateX are functions.
     
    vib, May 18, 2006
    #1
    1. Advertising

  2. vib

    Jack Klein Guest

    On 17 May 2006 18:42:03 -0700, "vib" <> wrote in
    comp.lang.c:

    > Hi there,
    >
    > I wish to get some advice on the problem I face in initializing a
    > table.
    >
    > Here is the code. I've an array of strings, ptrNameString . I want to
    > initialize a table, mySoftKeyTable with some contents of the string
    > from the array. This initialization is not in any function, but rather
    > outside of any function. However, the compiler complaints, the related
    > lines( L1, L2, L3, L4) of "Initializer element is not constant". I
    > guess, it expects a constant but getting a non-constant instead.
    >
    > Please advise.


    The first thing that I advise is to rewrite this code, it is
    unreadable. Remove the commented out material. I had to paste this
    into a syntax coloring editor to begin to get a grip on it, and hope
    the coloring is right.

    > Thanks in advance.
    > vibnwis
    >
    > char * ptrNameString [5] = { "One", "Two", "Three", "Four", "Five" };
    >
    > WWM_SOFTKEY_ITEM_DATA mySoftKeyTable[] =


    What in the heck is a "WWM_SOFTKEY_ITEM_DATA"? What are its
    components?


    > /* L1 */ {{
    > /*adrStrings[STR_SETUP].textStr[ENGLISH]*/*(ptrNameString),
    > (void (* ) (void *)) winHelloCreate, UGL_NULL, 0},


    If my editor colored your code correctly, after replacing comments
    with a single white space, the three lines above are:

    {{
    *(ptrNameString),
    (void (* ) (void *)) winHelloCreate, UGL_NULL, 0},

    ....is that correct?

    In that case, I am guessing that the first member of a
    "WWM_SOFTKEY_ITEM_DATA" is a char, or at least an integer type?

    In any case, it appears that you are trying to initialize this member
    of the first "WWM_SOFTKEY_ITEM_DATA" in the array with:

    *(prtNameString)

    ....and you can't do that. Objects at file scope must be initialized
    with constant expressions, and the value of an object is not a
    constant expression in C.

    If you want to initialize the first member to the letter '0', just
    write 'O' as an initializer in the source.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 18, 2006
    #2
    1. Advertising

  3. vib

    vib Guest

    hi Jack

    Many thanks for your time and efforts. The comments were actually
    added for the reasons to enhance my explanation. They were not there in
    my code.

    Yeah. I am agree with you. Boss is asking for a easy-understand table (
    not initialize it in runtime). I am cracking my head with the
    solution. Apparently not working.
    Is there any workaround, so that I can initialize the table before
    getting into runtime.

    Thanks
     
    vib, May 18, 2006
    #3
  4. vib

    pete Guest

    vib wrote:
    >
    > hi Jack
    >
    > Many thanks for your time and efforts. The comments were actually
    > added for the reasons to enhance my explanation. They were not there in
    > my code.
    >
    > Yeah. I am agree with you. Boss is asking for a easy-understand table (
    > not initialize it in runtime). I am cracking my head with the
    > solution. Apparently not working.
    > Is there any workaround, so that I can initialize the table before
    > getting into runtime.


    Change the type of the first member of the structure, to char **

    /* BEGIN new.c */

    #include <stdio.h>

    char * ptrNameString [] = { "One", "Two", "Three", "Four", "Five" };

    char **pointer = ptrNameString+2;

    int main(void)
    {
    puts(*pointer);
    return 0;
    }

    /* END new.c */

    --
    pete
     
    pete, May 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd Nathan

    Error: "initializer element is not constant"

    Todd Nathan, Jul 30, 2003, in forum: C Programming
    Replies:
    2
    Views:
    19,375
    Kevin Easton
    Jul 30, 2003
  2. B. Wood
    Replies:
    7
    Views:
    1,135
    Glen Herrmannsfeldt
    Oct 17, 2003
  3. Levi Campbell
    Replies:
    3
    Views:
    417
    Flash Gordon
    Feb 11, 2006
  4. Replies:
    3
    Views:
    635
    Wade Ward
    Oct 21, 2007
  5. Gowtham
    Replies:
    16
    Views:
    812
    Szabolcs Borsanyi
    May 9, 2008
Loading...

Share This Page