What is the meaing of this typedef line

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Line
AI Thread Summary
The discussion centers on the use of `typedef` in C programming, particularly in defining structures for tree nodes. The primary argument is that `typedef` simplifies the declaration of types, making code more readable and manageable. It allows programmers to create new type names, which can enhance portability and documentation clarity. The conversation highlights that in C, one must use the `struct` keyword when declaring instances unless a `typedef` is used, which can reduce redundancy. There is also a debate about the necessity of using `typedef` in certain contexts, especially when transitioning from C to C++, where the use of classes eliminates the need for `typedef`. The discussion includes examples of how to properly declare structures and pointers, emphasizing that using `typedef` can help avoid confusion with naming conventions. Additionally, the conversation touches on memory allocation in C, comparing it to C++'s `new` keyword, and clarifies that `malloc` or `calloc` should be used in C for dynamic memory allocation.
transgalactic
Messages
1,386
Reaction score
0
Code:
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
 
Technology news on Phys.org
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:

Besides purely aesthetic issues, there are two main reasons for using typedefs.
The first is to parametrize a program against portability problems. ...
The second purpose of typedefs is to provide better documentation for a program - a type called Treeptr may be easier to understand than one declared only as a pointer to a complicated structure
 
In C, if you want to define a struct, and later declare instances of that struct, one has to do:
Code:
struct Foo { int bar; //... };
struct Foo foo_instance;
Alternatively one can write:
Code:
typedef struct Foo { int bar; //... } Foo_t;
Foo_t foo_instance;
which is equivalent to:
Code:
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:
struct { int bar; //... } foo_instance, another_foo_instance;
or by giving the struct a name after all with a typedef:
Code:
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:
struct Foo { int bar; //... };
Foo hello_foo;
 
KTC said:
In C, if you want to define a struct, and later declare instances of that struct, one has to do:
Code:
struct Foo { int bar; //... };
struct Foo foo_instance;
Alternatively one can write:
Code:
typedef struct Foo { int bar; //... } Foo_t;
Foo_t foo_instance;
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:
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:
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:
transgalactic said:
Code:
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

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.
 
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:
typedef tnode *TreePtr;
typedef struct tnode{
  int data;
  TreePtr left;
  TreePtr right;
}TreeNode;
 
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?
 
mathmate said:
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:
typedef tnode *TreePtr;
typedef struct tnode{
  int data;
  TreePtr left;
  TreePtr right;
}TreeNode;

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:
struct tnode;                    //***************Add this!
typedef tnode *TreePtr;
typedef struct tnode{
  int data;
  TreePtr left;
  TreePtr right;
}TreeNode;
 
zyh said:
but this works:

Code:
struct tnode;                    //***************Add this!
typedef tnode *TreePtr;
typedef struct tnode{
  int data;
  TreePtr left;
  TreePtr right;
}TreeNode;

That shouldn't work, either. "tnode" is not a type. This is valid standard C:
Code:
typedef struct tnode *TreePtr;
typedef struct tnode{
  int data;
  TreePtr left;
  TreePtr right;
}TreeNode;
As is this:
Code:
typedef struct tnode TreeNode, *TreePtr;
struct tnode{
  int data;
  TreeNode * left;
  TreePtr right;
};
 
HallsofIvy said:
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?

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 don't know how to do new command in C
 
  • #10
transgalactic said:
you would need

struct TreeNode * newnode;
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;

and then to make a command "new" in order to allocate space for the root
but its c++ command
i don't know how to do new command in C
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.
 
  • #11
how to do "new " at c using calloc/malloc

in c++ i have
Code:
root=new node;

how do i write that in C?
 
  • #12
  • #13
this is a normal malloc variable
so i suppose the answer for the question will be:

root=malloc(node);
 
Back
Top