Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15858

Re: Tree implementation, ideas for keyboard input method needed

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Tree implementation, ideas for keyboard input method needed
Date 2012-01-16 14:27 -0800
Organization None to speak of
Message-ID <lnzkdnz3j3.fsf@nuthaus.mib.org> (permalink)
References <Xns9FDCEA347419Bbubbachipsetone@130.133.4.11>

Show all headers | View raw


Bubba <nickname@banelli.biz.invalid> writes:

Just so you know, I'm not going to address your actual question, at
least for now.  I do have some comments on your coding style.

> nodes of my ordered tree are implemented like this:
>
>     typedef struct __celltype {
>           labeltype label;
>           struct __celltype *first_child;
>           struct __celltype *next_sib;
>           struct __celltype *last_child;
>           int zadnji;
>     } celltype;

Identifiers starting with underscores are reserved to the implementation
in most contexts.  Identifiers starting with two underscores are
reserved in all contexts.  You should never define such identifiers
yourself (unless you're writing code for a C compiler or library
implementation).

It's not necessary to use a typedef for a struct type.  You could just
declare it as:

    struct celltype {
        ...
    };

and simply refer to it as "struct celltype".  If you feel the need to
have a one-word name for the type, there's no need to use different
identifiers for the tag and typedef:

    typedef struct celltype {
        ...
    } celltype;

>     typedef celltype *node;
>     typedef celltype *TREE;

Typedefs for pointer types are generally a bad idea.  The problem is
that they hide the fact that the type is a pointer.  That's ok if it's a
truly opaque type, meaning that client code will not use any pointer
operations on it.

And logically, a pointer to a "celltype" isn't a node; it points to a node.

[...]
> node MAKE_ROOT(labeltype l, TREE *T) {

All-caps names are conventionally used for macros.  It's better style
to call the function "make_root".

And in fact I'd declare this as:

    struct celltype *make_root(labeltype label, struct celltype **t);

Note that "l" is a poor identifier; it's too easily confused with the
digit "1".

>      if (!(*T = (celltype*) malloc(sizeof(celltype)))) {

The cast is unnecessary, and could be harmful (in certain circumstances,
it can inhibit important warnings).  malloc() returns a void* result,
which can be implicitly converted to any pointer type (other than a
pointer-to-function type).

A good idiom is:

    if ((*T = malloc(sizeof **T) != NULL) {

>     printf("Nema dovoljno memorije.");

Error mesages should be printed to stderr and terminated with "\n".

>     exit(1);

exit(1) doesn't necessarily indicate failure.  Use exit(EXIT_FAILURE)
instead.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Tree implementation, ideas for keyboard input method needed Bubba <nickname@banelli.biz.invalid> - 2012-01-16 22:01 +0000
  Re: Tree implementation, ideas for keyboard input method needed Keith Thompson <kst-u@mib.org> - 2012-01-16 14:27 -0800
    Re: Tree implementation, ideas for keyboard input method needed Bubba <nickname@banelli.biz.invalid> - 2012-01-16 22:36 +0000
      Re: Tree implementation, ideas for keyboard input method needed Kaz Kylheku <kaz@kylheku.com> - 2012-01-16 22:53 +0000
      Re: Tree implementation, ideas for keyboard input method needed James Kuyper <jameskuyper@verizon.net> - 2012-01-16 18:06 -0500
      Re: Tree implementation, ideas for keyboard input method needed Keith Thompson <kst-u@mib.org> - 2012-01-16 16:41 -0800
    Re: Tree implementation, ideas for keyboard input method needed Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-16 23:01 +0000
      Re: Tree implementation, ideas for keyboard input method needed Shao Miller <sha0.miller@gmail.com> - 2012-01-16 19:49 -0500
        Re: Tree implementation, ideas for keyboard input method needed Keith Thompson <kst-u@mib.org> - 2012-01-16 17:46 -0800
  Re: Tree implementation, ideas for keyboard input method needed Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-16 22:29 +0000
    Re: Tree implementation, ideas for keyboard input method needed Bubba <nickname@banelli.biz.invalid> - 2012-01-16 22:38 +0000
    Re: Tree implementation, ideas for keyboard input method needed William Ahern <william@wilbur.25thandClement.com> - 2012-01-16 15:30 -0800
      Re: Tree implementation, ideas for keyboard input method needed Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-17 00:03 +0000
      Re: Tree implementation, ideas for keyboard input method needed Keith Thompson <kst-u@mib.org> - 2012-01-16 16:35 -0800
        Re: Tree implementation, ideas for keyboard input method needed cri@tiac.net (Richard Harter) - 2012-01-17 02:00 +0000
          Re: Tree implementation, ideas for keyboard input method needed Keith Thompson <kst-u@mib.org> - 2012-01-16 19:16 -0800
            Re: Tree implementation, ideas for keyboard input method needed cri@tiac.net (Richard Harter) - 2012-01-17 05:15 +0000
              Re: Tree implementation, ideas for keyboard input method needed Keith Thompson <kst-u@mib.org> - 2012-01-17 00:00 -0800
          Re: Tree implementation, ideas for keyboard input method needed Kaz Kylheku <kaz@kylheku.com> - 2012-01-17 04:04 +0000
  Re: Tree implementation, ideas for keyboard input method needed Kaz Kylheku <kaz@kylheku.com> - 2012-01-16 22:44 +0000
  Re: Tree implementation, ideas for keyboard input method needed Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-16 17:58 -0500
  Re: Tree implementation, ideas for keyboard input method needed ImpalerCore <jadill33@gmail.com> - 2012-01-16 19:36 -0800
    Re: Tree implementation, ideas for keyboard input method needed Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-01-16 23:33 -0500
      Re: Tree implementation, ideas for keyboard input method needed ImpalerCore <jadill33@gmail.com> - 2012-01-17 07:18 -0800
    Re: Tree implementation, ideas for keyboard input method needed "BartC" <bc@freeuk.com> - 2012-01-17 13:54 +0000
      Re: Tree implementation, ideas for keyboard input method needed ImpalerCore <jadill33@gmail.com> - 2012-01-17 07:39 -0800
    Re: Tree implementation, ideas for keyboard input method needed Kaz Kylheku <kaz@kylheku.com> - 2012-01-17 19:01 +0000
  Re: Tree implementation, ideas for keyboard input method needed jgk@panix.com (Joe keane) - 2012-01-18 21:21 +0000

csiph-web