What is the meaing of this typedef line

  • Thread starter transgalactic
  • Start date
  • Tags
    Line
In summary, typedef is a facility in C that allows for the creation of new data type names. It can be used to simplify coding, reduce the appearance of the struct keyword, and provide better documentation for a program. The use of typedef in C++ is equivalent to using the class name, making typedef unnecessary. However, in C, it serves as a way to define a struct without using a tag name and allows for the declaration of instances of that struct without the struct keyword.
  • #1
transgalactic
1,395
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
  • #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:

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
 
  • #3
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;
 
  • #4
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.
 
  • #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:
typedef tnode *TreePtr;
typedef struct tnode{
  int data;
  TreePtr left;
  TreePtr right;
}TreeNode;
 
  • #6
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?
 
  • #7
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;
 
  • #8
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;
};
 
  • #9
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?
 
  • #13
this is a normal malloc variable
so i suppose the answer for the question will be:

root=malloc(node);
 

1. What is a typedef line?

A typedef line is a declaration in programming that allows the user to create an alias for a data type. This alias can then be used instead of the original data type in the code, making it easier to read and understand.

2. Why is a typedef line used?

A typedef line is used to improve the readability and maintainability of code. By creating an alias for a data type, the code becomes more self-documenting and easier to understand for both the programmer and others who may read the code.

3. How is a typedef line written?

A typedef line typically follows this format:
typedef ;
For example: typedef int myInt;
This creates an alias "myInt" for the data type "int".

4. Can a typedef line be used for more than one data type?

Yes, a typedef line can be used to create aliases for multiple data types. Each alias should be declared on a separate line.

5. What are the benefits of using a typedef line?

Aside from improving code readability, typedef lines also make it easier to change data types in the future. If the original data type needs to be changed, only the typedef line needs to be updated, rather than every instance of the data type in the code. This can save time and reduce the chances of errors.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
2
Replies
52
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
Replies
10
Views
959
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top