Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Describing a tree

Reply
Thread Tools

Describing a tree

 
 
Nick Keighley
Guest
Posts: n/a
 
      07-21-2010
On 21 July, 09:16, Ian Collins <ian-n...@hotmail.com> wrote:
> On 07/21/10 07:17 PM, Nick Keighley wrote:
>
>
>
>
>
> > On 21 July, 07:54, "io_x"<a...@b.c.invalid> *wrote:
> >> "Nick Keighley"<nick_keighley_nos...@hotmail.com> *ha scritto nel messaggionews:e8246c02-b7d1-4ce9-9bd9-...
> >>> On 20 July, 13:10, Mok-Kong Shen<mok-kong.s...@t-online.de> *wrote:

>
> >>>> A possibly dumb question: Given a labeled or unlabeled tree
> >>>> (in the sense of graphs), how could one best use C to describe it?

>
> >>> what form is it in now or how are you going to enter it?

>
> >>> /* tree.c */

>
> >>> #include<stdio.h>

>
> >>> typedef struct tree_tag
> >>> {
> >>> * * char *data;
> >>> * * struct tree_tag *left;
> >>> * * struct tree_tag *right;
> >>> } Tree;

>
> >>> void walk (Tree *tree)
> >>> {
> >>> * * if (tree != NULL)
> >>> * * {
> >>> * * * * printf ("%s ", tree->data);
> >>> * * * * walk (tree->left);
> >>> * * * * walk (tree->right);
> >>> * * }
> >>> }

>
> >>> int main (void)
> >>> {
> >>> * * Tree yggdrasil[] =
> >>> * * {
> >>> * * * * {"red",&yggdrasil[1],&yggdrasil[2]},
> >>> * * * * {"blue",&yggdrasil[3],&yggdrasil[4]},
> >>> * * * * {"black", 0, 0},
> >>> * * * * {"yellow", 0, 0},
> >>> * * * * {"green", 0, 0}
> >>> * * };

>
> >>> * * walk (yggdrasil);
> >>> * * return 0;
> >>> }

>
> >> Error E2063 tree1.c 27: Illegal initialization in function main
> >> *** 1 errors in Compile ***

>
> > I'm surprised! It compiled and executed ok for me with an old Windows
> > compiler. And on gcc unless I stuck -pedantic on... ok fair cop

>
> It's perfectly valid C.


ah yes, even with pedantic I only got

D:\bin\net>gcc -W -Wall -pedantic -ansi tree.c
tree.c: In function `main':
tree.c:27: warning: initializer element is not computable at load time
[several times]




 
Reply With Quote
 
 
 
 
Ersek, Laszlo
Guest
Posts: n/a
 
      07-21-2010
On Tue, 20 Jul 2010, Alan Curry wrote:

> struct node {
> const char *label;
> const struct node *left, *right;
> };
>
> const struct node mytree[] = {
> {"root", &mytree[1], &mytree[2]},
> {"left", &mytree[3], 0},
> {"right", 0, &mytree[4]},
> {"leftleft", 0, 0},
> {"rightright", 0, 0}
> };


This is great!

Inside the initializer, the array object ("mytree") has incomplete type
and is not yet defined. So one couldn't use "sizeof mytree", for example.
However, since "mytree" is already declared and it decays to a pointer to
its first element, "mytree + offset" works.

Thanks,
lacos
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-21-2010
Nick Keighley <> writes:

> On 21 July, 09:16, Ian Collins <ian-n...@hotmail.com> wrote:
>> On 07/21/10 07:17 PM, Nick Keighley wrote:
>> > On 21 July, 07:54, "io_x"<a...@b.c.invalid> Â*wrote:
>> >> "Nick Keighley"<nick_keighley_nos...@hotmail.com> Â*ha scritto nel messaggionews:e8246c02-b7d1-4ce9-9bd9-...
>> >>> On 20 July, 13:10, Mok-Kong Shen<mok-kong.s...@t-online.de> Â*wrote:

>>
>> >>>> A possibly dumb question: Given a labeled or unlabeled tree
>> >>>> (in the sense of graphs), how could one best use C to describe it?

>>
>> >>> what form is it in now or how are you going to enter it?

>>
>> >>> /* tree.c */

>>
>> >>> #include<stdio.h>

>>
>> >>> typedef struct tree_tag
>> >>> {
>> >>> Â* Â* char *data;
>> >>> Â* Â* struct tree_tag *left;
>> >>> Â* Â* struct tree_tag *right;
>> >>> } Tree;

>>
>> >>> void walk (Tree *tree)
>> >>> {
>> >>> Â* Â* if (tree != NULL)
>> >>> Â* Â* {
>> >>> Â* Â* Â* Â* printf ("%s ", tree->data);
>> >>> Â* Â* Â* Â* walk (tree->left);
>> >>> Â* Â* Â* Â* walk (tree->right);
>> >>> Â* Â* }
>> >>> }

>>
>> >>> int main (void)
>> >>> {
>> >>> Â* Â* Tree yggdrasil[] =
>> >>> Â* Â* {
>> >>> Â* Â* Â* Â* {"red",&yggdrasil[1],&yggdrasil[2]},
>> >>> Â* Â* Â* Â* {"blue",&yggdrasil[3],&yggdrasil[4]},
>> >>> Â* Â* Â* Â* {"black", 0, 0},
>> >>> Â* Â* Â* Â* {"yellow", 0, 0},
>> >>> Â* Â* Â* Â* {"green", 0, 0}
>> >>> Â* Â* };

>>
>> >>> Â* Â* walk (yggdrasil);
>> >>> Â* Â* return 0;
>> >>> }

>>
>> >> Error E2063 tree1.c 27: Illegal initialization in function main
>> >> *** 1 errors in Compile ***

>>
>> > I'm surprised! It compiled and executed ok for me with an old Windows
>> > compiler. And on gcc unless I stuck -pedantic on... ok fair cop

>>
>> It's perfectly valid C.


I'd say that it is not. An implementation is permitted to reject the
code which goes against what most people would call perfectly valid C.

> ah yes, even with pedantic I only got
>
> D:\bin\net>gcc -W -Wall -pedantic -ansi tree.c
> tree.c: In function `main':
> tree.c:27: warning: initializer element is not computable at load time
> [several times]


That does not tell you if it is or is not valid C!

The standard says that when an address constant references an object it
much be a static object. I.e. a conforming C implementation can reject
(but, as it happens, need not reject) the program. Is that "perfectly
valid C"? I'd say not, although it is OK in the sense that an
implementation may permit other forms of constant expression so there is
no reason that it should not be accepted. If you use that sort of
construct your code won't always compile on other systems.

--
Ben.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-21-2010
On 07/22/10 01:00 AM, Ben Bacarisse wrote:
> Nick Keighley<> writes:
>> On 21 July, 09:16, Ian Collins<ian-n...@hotmail.com> wrote:
>>> On 07/21/10 07:17 PM, Nick Keighley wrote:
>>>
>>>>>> int main (void)
>>>>>> {
>>>>>> Tree yggdrasil[] =
>>>>>> {
>>>>>> {"red",&yggdrasil[1],&yggdrasil[2]},
>>>>>> {"blue",&yggdrasil[3],&yggdrasil[4]},
>>>>>> {"black", 0, 0},
>>>>>> {"yellow", 0, 0},
>>>>>> {"green", 0, 0}
>>>>>> };
>>>
>>>
>>> It's perfectly valid C.

>
> I'd say that it is not. An implementation is permitted to reject the
> code which goes against what most people would call perfectly valid C.
>
>> ah yes, even with pedantic I only got
>>
>> D:\bin\net>gcc -W -Wall -pedantic -ansi tree.c
>> tree.c: In function `main':
>> tree.c:27: warning: initializer element is not computable at load time
>> [several times]

>
> That does not tell you if it is or is not valid C!


If you compile for the correct version of C, it is.

> The standard says that when an address constant references an object it
> much be a static object. I.e. a conforming C implementation can reject
> (but, as it happens, need not reject) the program. Is that "perfectly
> valid C"? I'd say not, although it is OK in the sense that an
> implementation may permit other forms of constant expression so there is
> no reason that it should not be accepted. If you use that sort of
> construct your code won't always compile on other systems.


Both C99 compilers I tried are happy with the code, C89 compilers
complained.

--
Ian Collins
 
Reply With Quote
 
Mok-Kong Shen
Guest
Posts: n/a
 
      07-21-2010
Nick Keighley:

>> Actually I first came to the issue from a rather humble goal: Simply
>> to describe something of the genre of natural trees,

>
> "natural trees"!? You mean like The Larch? What properties natural
> trees do you wish to describe? Phylogeny? Morphology? Leaf count?
> Beetle count?


I don't know enough to correctly answer in your terms. An example of
what I thoght of is a tree structure that is commonly seen in
literatures on evolution of the diverse biological species.

M. K. Shen

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-21-2010
Ian Collins <ian-> writes:

> On 07/22/10 01:00 AM, Ben Bacarisse wrote:
>> Nick Keighley<> writes:
>>> On 21 July, 09:16, Ian Collins<ian-n...@hotmail.com> wrote:
>>>> On 07/21/10 07:17 PM, Nick Keighley wrote:
>>>>
>>>>>>> int main (void)
>>>>>>> {
>>>>>>> Tree yggdrasil[] =
>>>>>>> {
>>>>>>> {"red",&yggdrasil[1],&yggdrasil[2]},
>>>>>>> {"blue",&yggdrasil[3],&yggdrasil[4]},
>>>>>>> {"black", 0, 0},
>>>>>>> {"yellow", 0, 0},
>>>>>>> {"green", 0, 0}
>>>>>>> };
>>>>
>>>>
>>>> It's perfectly valid C.

>>
>> I'd say that it is not. An implementation is permitted to reject the
>> code which goes against what most people would call perfectly valid C.
>>
>>> ah yes, even with pedantic I only got
>>>
>>> D:\bin\net>gcc -W -Wall -pedantic -ansi tree.c
>>> tree.c: In function `main':
>>> tree.c:27: warning: initializer element is not computable at load time
>>> [several times]

>>
>> That does not tell you if it is or is not valid C!

>
> If you compile for the correct version of C, it is.


Sorry, I don't follow you. All I am saying is that a warning from gcc
does not tell you if the code is valid.

The error is not a constraint violation so a compiler need not say a
word about it. Had gcc been silent on the matter you could not conclude
that the code is fine. Equally, gcc is permitted to warn about anything
it likes, so a warning does not mean that the code is not valid.

>> The standard says that when an address constant references an object it
>> much be a static object. I.e. a conforming C implementation can reject
>> (but, as it happens, need not reject) the program. Is that "perfectly
>> valid C"? I'd say not, although it is OK in the sense that an
>> implementation may permit other forms of constant expression so there is
>> no reason that it should not be accepted. If you use that sort of
>> construct your code won't always compile on other systems.

>
> Both C99 compilers I tried are happy with the code, C89 compilers
> complained.


I still don't get your point. It's good that the C89 compilers
complained but what do you mean by pointing out that the C99 ones are
happy with it? Does that lead you to believe that there is no problem
with the construct?

No C99 compiler need diagnose the error (well, lets's call it an
extension rather than an error) so the fact that they seem happy does
not tell you much about the validity of the construct.

--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-21-2010
Ian Collins <ian-> writes:

> On 07/22/10 01:00 AM, Ben Bacarisse wrote:
>> Nick Keighley<> writes:
>>> On 21 July, 09:16, Ian Collins<ian-n...@hotmail.com> wrote:
>>>> On 07/21/10 07:17 PM, Nick Keighley wrote:
>>>>
>>>>>>> int main (void)
>>>>>>> {
>>>>>>> Tree yggdrasil[] =
>>>>>>> {
>>>>>>> {"red",&yggdrasil[1],&yggdrasil[2]},
>>>>>>> {"blue",&yggdrasil[3],&yggdrasil[4]},
>>>>>>> {"black", 0, 0},
>>>>>>> {"yellow", 0, 0},
>>>>>>> {"green", 0, 0}
>>>>>>> };
>>>>
>>>>
>>>> It's perfectly valid C.

>>
>> I'd say that it is not. An implementation is permitted to reject the
>> code which goes against what most people would call perfectly valid C.
>>
>>> ah yes, even with pedantic I only got
>>>
>>> D:\bin\net>gcc -W -Wall -pedantic -ansi tree.c
>>> tree.c: In function `main':
>>> tree.c:27: warning: initializer element is not computable at load time
>>> [several times]

>>
>> That does not tell you if it is or is not valid C!

>
> If you compile for the correct version of C, it is.


Yup. Scratch my last reply. Trying to read to many things at once I
got my versions all mixed. I'd summarise what I think is the case about
this only I am sure I'd make even more confusion.

It's fine in C99.

--
Ben.
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-21-2010
Ben Bacarisse <> writes:

> Ian Collins <ian-> writes:
>
>> On 07/22/10 01:00 AM, Ben Bacarisse wrote:
>>> Nick Keighley<> writes:
>>>> On 21 July, 09:16, Ian Collins<ian-n...@hotmail.com> wrote:
>>>>> On 07/21/10 07:17 PM, Nick Keighley wrote:
>>>>>
>>>>>>>> int main (void)
>>>>>>>> {
>>>>>>>> Tree yggdrasil[] =
>>>>>>>> {
>>>>>>>> {"red",&yggdrasil[1],&yggdrasil[2]},
>>>>>>>> {"blue",&yggdrasil[3],&yggdrasil[4]},
>>>>>>>> {"black", 0, 0},
>>>>>>>> {"yellow", 0, 0},
>>>>>>>> {"green", 0, 0}
>>>>>>>> };
>>>>>
>>>>>
>>>>> It's perfectly valid C.
>>>
>>> I'd say that it is not. An implementation is permitted to reject the
>>> code which goes against what most people would call perfectly valid C.
>>>
>>>> ah yes, even with pedantic I only got
>>>>
>>>> D:\bin\net>gcc -W -Wall -pedantic -ansi tree.c
>>>> tree.c: In function `main':
>>>> tree.c:27: warning: initializer element is not computable at load time
>>>> [several times]
>>>
>>> That does not tell you if it is or is not valid C!

>>
>> If you compile for the correct version of C, it is.

>
> Sorry, I don't follow you. All I am saying is that a warning from gcc
> does not tell you if the code is valid.


For the record: I follow now! This is a C89 /C99 difference (though I
can't claim that was why I said what I said -- in fact I have no idea
*what* I was thinking!).

> The error is not a constraint violation so a compiler need not say a
> word about it. Had gcc been silent on the matter you could not conclude
> that the code is fine. Equally, gcc is permitted to warn about anything
> it likes, so a warning does not mean that the code is not valid.


This is all correct but it clouds the issue because the code is, in
fact, fine.

<snip>
>> Both C99 compilers I tried are happy with the code, C89 compilers
>> complained.

>
> I still don't get your point. It's good that the C89 compilers
> complained but what do you mean by pointing out that the C99 ones are
> happy with it? Does that lead you to believe that there is no problem
> with the construct?


As indeed you should!

Sorry for the confusion. I hope this correction is fast enough to
prevent the confusion spreading any further.

<snip>
--
Ben.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      07-21-2010
On 07/22/10 11:24 AM, Ben Bacarisse wrote:
> On 07/22/10 01:00 AM, Ben Bacarisse wrote:
>> Ian Collins<ian-> writes:


>>>> That does not tell you if it is or is not valid C!
>>>
>>> If you compile for the correct version of C, it is.

>>
>> Sorry, I don't follow you. All I am saying is that a warning from gcc
>> does not tell you if the code is valid.

>
> For the record: I follow now! This is a C89 /C99 difference (though I
> can't claim that was why I said what I said -- in fact I have no idea
> *what* I was thinking!).
>
>> The error is not a constraint violation so a compiler need not say a
>> word about it. Had gcc been silent on the matter you could not conclude
>> that the code is fine. Equally, gcc is permitted to warn about anything
>> it likes, so a warning does not mean that the code is not valid.

>
> This is all correct but it clouds the issue because the code is, in
> fact, fine.
>
> <snip>
>>> Both C99 compilers I tried are happy with the code, C89 compilers
>>> complained.

>>
>> I still don't get your point. It's good that the C89 compilers
>> complained but what do you mean by pointing out that the C99 ones are
>> happy with it? Does that lead you to believe that there is no problem
>> with the construct?

>
> As indeed you should!
>
> Sorry for the confusion. I hope this correction is fast enough to
> prevent the confusion spreading any further.


Just in time. I was composing a reply when yours popped up!

I'm sorry I was so vague in my earlier replies, if I'd just said "It's
perfectly valid C99", much bandwidth would have been saved.

--
Ian Collins
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-22-2010
Ian Collins <ian-> writes:
<snip>
> Just in time. I was composing a reply when yours popped up!
>
> I'm sorry I was so vague in my earlier replies, if I'd just said "It's
> perfectly valid C99", much bandwidth would have been saved.


I wish I could accept that gracious thought, but you did say that --
word for word! There is no need to think you should have to repeat
yourself just because I was having some kind of brain burp.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Describing pipelined hardware Jonathan Bromley VHDL 50 06-22-2006 03:23 PM
describing a file system in xml uvts_cvs@yahoo.com XML 0 03-11-2005 10:02 AM
What am I describing? Gloria Goitre Computer Support 5 02-12-2005 04:05 AM
syntax/notation used in describing c's grammar ben C Programming 4 08-20-2004 07:38 PM
B tree, B+ tree and B* tree Stub C Programming 3 11-12-2003 01:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57