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

H

hankypan1

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
-
 
K

keith

#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.
 
C

CBFalconer

.... 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.
 
W

Wade Ward

CBFalconer said:
... 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
(e-mail address removed)
"Der Katze tritt die Treppe hoch; Der Kater tritt sie krumm.%
% De Teufel geit um; er bringt de menschen allet dumm."
schau, schau
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top