469,129 Members | 1,721 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,129 developers. It's quick & easy.

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

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 '07 #1
2 4690
On 15 Oct, 09:44, hankyp...@gmail.com 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 '07 #2
ha*******@gmail.com 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

Oct 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Todd Nathan | last post: by
4 posts views Thread by floppyzedolfin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.