What is the meaing of this typedef line

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Line
Click For Summary

Discussion Overview

The discussion revolves around the use of the `typedef` keyword in C programming, particularly in the context of defining structures and their instances. Participants explore the implications of using `typedef` for creating new type names, its necessity, and its impact on code readability and portability.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants question the necessity of `typedef` in defining a structure, suggesting it complicates the building of a data type.
  • Others explain that `typedef` allows for the creation of new data type names, which can enhance code readability and portability.
  • One participant describes how `typedef` can reduce the need to repeatedly use the `struct` keyword, simplifying declarations.
  • There is a discussion about naming conventions, such as the use of the `_t` suffix to avoid confusion, with some arguing that it is superfluous.
  • Several participants provide examples of valid struct definitions and the implications of using or omitting type names.
  • Some participants express confusion over how to declare instances of types defined with `typedef`, leading to further clarification on syntax.
  • There is a mention of the differences between C and C++ regarding struct definitions and instance declarations.
  • Participants discuss memory allocation in C, comparing it to C++ and seeking clarification on how to use `malloc` or `calloc` for dynamic memory allocation.

Areas of Agreement / Disagreement

Participants express a range of views on the use of `typedef`, with no clear consensus on its necessity or best practices. Some agree on its benefits for readability, while others question its use in certain contexts.

Contextual Notes

Some participants highlight limitations in understanding due to the complexity of struct definitions and the nuances of type declarations in C versus C++. There are also unresolved questions regarding specific syntax and memory allocation practices.

Who May Find This Useful

Readers interested in C programming, particularly those learning about data structures, type definitions, and memory management, may find this discussion beneficial.

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);
 

Similar threads

Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
6
Views
2K
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
8K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 52 ·
2
Replies
52
Views
4K