Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

What is the meaing of this typedef line

  1. Oct 26, 2008 #1
    Code (Text):

    typedef struct object{
      int data;
      struct object *left;
     struct object  *right;
    }object;

     
    this is a code for the root of a tree
    why do they add typedef
    it makes no sense using it in the building of a data type

    and there is no two names after the struct
     
  2. jcsd
  3. Oct 26, 2008 #2
    Typedef is a facility to create new data type name in C. If you are writing code in C++, the equivalent of typedef is the class name, so typedef is superfluous, if legal in syntax at all.

    Assuming you are writing in C, you can read up section 6.7 of Kernighan and Richie's "The C Programming Language" which describes all the details, and from which I paraphrase the last paragraph for your information:

     
  4. Oct 26, 2008 #3

    KTC

    User Avatar

    In C, if you want to define a struct, and later declare instances of that struct, one has to do:
    Code (Text):
    struct Foo { int bar; //... };
    struct Foo foo_instance;
    Alternatively one can write:
    Code (Text):
    typedef struct Foo { int bar; //... } Foo_t;
    Foo_t foo_instance;
    which is equivalent to:
    Code (Text):
    struct Foo { int bar; //... };
    typedef struct Foo Foo_t;
    Foo_t foo_instance;
    It is also possible to declare a struct without a tag name (i.e. Foo). In which case, one has to declare the instance(s) of that struct at the struct definition:
    Code (Text):
    struct { int bar; //... } foo_instance, another_foo_instance;
    or by giving the struct a name after all with a typedef:
    Code (Text):
    typedef struct { int bar; //... } Foo_t;
    Foo_t foo_instance_again;
    With C++, all the above would work as well, but isn't done in practice as one no longer has to prefix the declaration of instances with the struct keyword:
    Code (Text):
    struct Foo { int bar; //... };
    Foo hello_foo;
     
  5. Oct 26, 2008 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The "_t" in the typedef name "Foo_t" is superfluous here. The only reason to add that _t suffix is to avoid confusing human readers of the code. This is also valid:
    Code (Text):
    typedef struct Foo { int bar; //... } Foo;
    Foo foo_instance;
    In fact, so is this nice start for a candidate for the International Obfuscated C Contest:
    Code (Text):
    typedef struct Foo { int foo; double Foo; // ...} Foo;
    Foo foo;
    The above is perfectly valid because C has four namespace:
    1. Enum/struct/union tags. The "Foo" in "struct Foo" lives in this namespace.
    2. Struct/union members. The "int foo" and "double Foo" members of the Foo structure live in the Foo structure namespace.
    3. Labels. One could make the above IOCC candidate even more confusing by adding a Foo label to the code (and of course a goto Foo.)
    4. Ordinary identifiers. The typedef Foo and the variable foo live in this namespace.

    So, back to the OP:
    The typedef defines a new type. It makes a lot of sense, although one could argue that a less confusing choice of names might be a bit better. That is the mindset that leads to naming types with an _t suffix. On the other hand, some organizations explicitly mandate that the struct tag name and the corresponding typedef type name must be identical.
     
  6. Oct 26, 2008 #5
    There is another use of typedef that helps simplify coding by reducing the appearance of the struct keyword. This is to define the pointer as a type. For example, an example from K & R's book gives:
    Code (Text):

    typedef tnode *TreePtr;
    typedef struct tnode{
      int data;
      TreePtr left;
      TreePtr right;
    }TreeNode;
     
     
  7. Oct 27, 2008 #6

    HallsofIvy

    User Avatar
    Staff Emeritus
    Science Advisor

    So, in this, in this example, if I want to declare "newnode" to be of this type would I say
    tnode newnode;
    or
    TreeNode newnode?
     
  8. Oct 27, 2008 #7

    zyh

    User Avatar

    HI, mathmate, this code can't compile in my compiler.
    It reports that : error: identifier "tnode" is undefined
    typedef tnode *TreePtr;


    but this works:

    Code (Text):

    struct tnode;                    //***************Add this!
    typedef tnode *TreePtr;
    typedef struct tnode{
      int data;
      TreePtr left;
      TreePtr right;
    }TreeNode;
     
     
  9. Oct 27, 2008 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    That shouldn't work, either. "tnode" is not a type. This is valid standard C:
    Code (Text):
    typedef struct tnode *TreePtr;
    typedef struct tnode{
      int data;
      TreePtr left;
      TreePtr right;
    }TreeNode;
     
    As is this:
    Code (Text):
    typedef struct tnode TreeNode, *TreePtr;
    struct tnode{
      int data;
      TreeNode * left;
      TreePtr right;
    };
     
     
  10. Oct 27, 2008 #9
    you would need

    struct TreeNode * newnode;

    and then to make a command "new" in order to allocate space for the root
    but its c++ command
    i dont know how to do new command in C
     
  11. Oct 27, 2008 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    No! TreeNode is a typedef name. All you need is TreeNode * newnode; as a declaration. If you want to use the struct version, you must use the struct name: struct tnode * newnode;

    Of course you do. You just did it in another thread that you started. Use calloc or malloc. In fact, the C++ new command builds on top of malloc.
     
  12. Oct 27, 2008 #11
    how to do "new " at c using calloc/malloc

    in c++ i have
    Code (Text):

    root=new node;
     
    how do i write that in C?
     
  13. Oct 27, 2008 #12

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

  14. Oct 27, 2008 #13
    this is a normal malloc variable
    so i suppose the answer for the question will be:

    root=malloc(node);
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: What is the meaing of this typedef line
Loading...