Results 1 to 4 of 4

Thread: Simple C Question - typedef

  1. #1
    Join Date
    Nov 2010
    Beans
    29

    Simple C Question - typedef

    Hi all.
    I'm trying to build a tree, so in my header file I coded a struct for that. I have a little problem:

    Code:
    typedef struct tree_t *treeptr;
    typedef struct {
         ...
         treeptr right;
         treeptr left;
    }harbor_t;
    If a use like this, when I try to define a variable like
    Code:
    treeptr a;
    compiler goes crazy, says:
    "warning:initialization from incompatible pointer type"
    and
    error: dereferencing pointer to incomplete type" on the next line, when I try to reach an element of struct. (after malloc of course)

    On the other hand, when I try to use like this:
    Code:
    typedef struct tree_t *treeptr;
    typedef struct treeptr{
    .
    .
    };
    I can't define any variable for struct tree_t. Also, there is another warning in header file like:
    "warning: useless storage class specifier in empty declaration"

    Any suggestion?

  2. #2
    Join Date
    May 2006
    Beans
    1,787

    Re: Simple C Question - typedef

    Quote Originally Posted by AlGorism View Post
    Hi all.
    I'm trying to build a tree, so in my header file I coded a struct for that. I have a little problem:

    Code:
    typedef struct tree_t *treeptr;
    typedef struct {
         ...
         treeptr right;
         treeptr left;
    }harbor_t;
    If a use like this, when I try to define a variable like
    Code:
    treeptr a;
    compiler goes crazy, says:
    "warning:initialization from incompatible pointer type"
    and
    error: dereferencing pointer to incomplete type" on the next line, when I try to reach an element of struct. (after malloc of course)

    On the other hand, when I try to use like this:
    Code:
    typedef struct tree_t *treeptr;
    typedef struct treeptr{
    .
    .
    };
    I can't define any variable for struct tree_t. Also, there is another warning in header file like:
    "warning: useless storage class specifier in empty declaration"

    Any suggestion?
    What is the definition of struct tree_t?

  3. #3

    Re: Simple C Question - typedef

    In my opinion, typedef is way overused, especially in rookie code. The eight keystrokes you save typing 'treeptr' instead of 'struct tree_t *' are really negligible, and the second one is more clear by an order of magnitude. typedef should be used to make truly nasty declarations easier to read, not to make simple ones more obscure.

    In any case, you don't appear to have ever defined 'struct tree_t'. You should do that.

    Also, I might be misremembering, but I think the conventional reason for the _t suffix is to indicate that the name is a typedef. Your usage is backward. Of course, you're free to defy convention, but you'll only be confusing other people (and your future self) by doing so.

  4. #4
    Join Date
    Nov 2010
    Beans
    29

    Re: Simple C Question - typedef

    I solved it.
    I typed these in header file:
    Code:
    typedef struct tree_t tree_t;
    typedef struct tree_t* treeptr
    Then I defined my struct in c file. Something like this:
    Code:
    struct tree_t {
       .
       .
       treeptr left;
       treeptr right;
    It's working

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •