Global Data declaration : "error: initializer element is not constant"

Discussion in 'C Programming' started by hankypan1@gmail.com, Oct 15, 2007.

  1. Guest

    Hi All,

    I need a tree data structure for my application. It is the non -
    cyclic
    simple tree where i can have any number of children node and each
    child can recursively become a sub tree like a normal tree. Now the
    thing is i can popullate my tree at compile time like a global data.
    Since
    i know my tree definition at compile time, instead of using pointers
    to point to siblings or child nodes, i am planning to use the array
    with undefined length like int a[] = {1,2}.


    Issue is if i declare complete tree at one place like this, my code
    become unreadable and unmanageable because of nested and high depth
    of
    tree. Even if i format code with 2 white space, it will become very
    big with around 10-20 levels and 20 nodes at each level. Also
    declaring each node with some logical name instead of an array
    element
    at some position will also make it easy to modify in future when
    required.


    I was planning to declare each node seperately and then use that in
    tree as reference
    like
    Structure Node leaf1 = {1,1,1};
    Structure Node leaf2 = {1,1,1};
    Structure Node leaf3 = {1,1,1};


    and then
    Structure Node node[] = {1,1,1, leaf1, leaf2, leaf3};


    But the problem is i can not reference a variable like this as global
    data above my main method like this


    #include <stdio.h>


    Structure Node leaf1 = {1,1,1};
    Structure Node leaf2 = {1,1,1};
    Structure Node leaf3 = {1,1,1};


    Structure Node node[] = {leaf1, leaf2, leaf3};


    int main()
    {
    /// do processing;
    return 0;



    }


    It gives compilation error "error: initializer element is not
    constant". Pls let me know how can i handle such situation.

    Regards
    -
    , Oct 15, 2007
    #1
    1. Advertising

  2. Guest

    On 15 Oct, 09:44, wrote:
    > #include <stdio.h>
    >
    > Structure Node leaf1 = {1,1,1};
    > Structure Node leaf2 = {1,1,1};
    > Structure Node leaf3 = {1,1,1};
    >
    > Structure Node node[] = {leaf1, leaf2, leaf3};
    >
    > int main()
    > {
    > /// do processing;
    > return 0;
    >
    > }
    >
    > It gives compilation error "error: initializer element is not
    > constant". Pls let me know how can i handle such situation.


    That is not what it gives at all, unless you are using some hitherto
    unknown language rather than C.

    1. There is no "Structure" keyword in C.
    2. Even if you meant "struct", the datatype "struct Node" is undefined
    in your code.
    3. Given the above, I'm not even going to try and determine what the
    rest of the code means, if anything.
    , Oct 15, 2007
    #2
    1. Advertising

  3. CBFalconer Guest

    Re: Global Data declaration : "error: initializer element is notconstant"

    wrote:
    >

    .... snip ...
    >
    > #include <stdio.h>
    >
    > Structure Node leaf1 = {1,1,1};
    > Structure Node leaf2 = {1,1,1};
    > Structure Node leaf3 = {1,1,1};
    >
    > Structure Node node[] = {leaf1, leaf2, leaf3};
    >
    > int main() {
    > /// do processing;
    > return 0;
    > }
    >
    > It gives compilation error "error: initializer element is not
    > constant". Pls let me know how can i handle such situation.

    ^^^ ^

    Well, it isn't constant. It refers to variable objects.

    Please avoid these confusing misspellings, as underlined above.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 16, 2007
    #3
  4. Wade Ward Guest

    Re: Global Data declaration : "error: initializer element is not constant"

    "CBFalconer" <> wrote in message
    news:...
    > wrote:
    >>

    > ... snip ...
    >>
    >> #include <stdio.h>
    >>
    >> Structure Node leaf1 = {1,1,1};
    >> Structure Node leaf2 = {1,1,1};
    >> Structure Node leaf3 = {1,1,1};
    >>
    >> Structure Node node[] = {leaf1, leaf2, leaf3};
    >>
    >> int main() {
    >> /// do processing;
    >> return 0;
    >> }
    >>
    >> It gives compilation error "error: initializer element is not
    >> constant". Pls let me know how can i handle such situation.

    > ^^^ ^
    >
    > Well, it isn't constant. It refers to variable objects.
    >
    > Please avoid these confusing misspellings, as underlined above.

    You end with l big endian though. Yet in German, the reading order is
    different
    again: 42 is pronounced zwei und vierzig, big endian writing, little endian
    reading (for 2 digits only ;-)

    It depands on how well you love your dictionary. zwo un fertig.
    --
    wade ward

    "Der Katze tritt die Treppe hoch; Der Kater tritt sie krumm.%
    % De Teufel geit um; er bringt de menschen allet dumm."
    schau, schau
    Wade Ward, Oct 21, 2007
    #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. Noah
    Replies:
    5
    Views:
    944
  2. Todd Nathan

    Error: "initializer element is not constant"

    Todd Nathan, Jul 30, 2003, in forum: C Programming
    Replies:
    2
    Views:
    19,327
    Kevin Easton
    Jul 30, 2003
  3. Levi Campbell
    Replies:
    3
    Views:
    401
    Flash Gordon
    Feb 11, 2006
  4. TheFlyingDutchman

    Tiny C error - initializer element is not constant

    TheFlyingDutchman, Jan 25, 2011, in forum: C Programming
    Replies:
    4
    Views:
    956
    TheFlyingDutchman
    Jan 25, 2011
  5. Bart Vandewoestyne

    error: initializer element is not constant

    Bart Vandewoestyne, Oct 3, 2012, in forum: C Programming
    Replies:
    5
    Views:
    573
    Nobody
    Oct 4, 2012
Loading...

Share This Page