Typedef

Typedef

typedef is a keyword in the C and C++ programming languages. It is used to give a data type a new name. The intent is to make it easier for programmers to comprehend source code.

Usage examples

Consider this code:

int coxes;int jaffa;...coxes++;...if (jaffa = 10)...

Now consider this:

typedef int Apples;typedef int Oranges;Apples coxes;Oranges jaffa;...coxes++;...if (jaffa = 10)...

Both sections of code do the same thing. The use of typedef in the second example makes it easier to comprehend what is going on, namely, that one variable contains information about apples while the other contains information about oranges.

One more example:

struct var { int data1; int data2; char data3;};

Here a user-defined data type "var" has been defined. To create a variable of the type "var", in C, the following code is required:

struct var a;

Let's add the following line to the end of this example:

typedef struct var newtype;

Now, in order to create a variable of type "var", the following code will suffice:

newtype a;

The same can be accomplished with the following code:typedef struct var { int data1; int data2; char data3;}newtype;

Typedefs can also simplify creating pointers to self-referential structures. Consider this:

struct Node{int a; //Datastruct Node *nextptr;};

Normally, one would need to prefix an asterisk to each variable to assign it a pointer:

Node *startptr, *endptr, *curptr, *prevptr, errptr, *refptr;

A programmer would assume that errptr was indeed a Node *, but a typographical error means that errptr is a Node. This can lead to subtle syntax errors.

It would be better to define a Node * type. The following code does so.

typedef Node *NodePtr;...NodePtr startptr, endptr, curptr, prevptr, errptr, refptr;

Now it is guaranteed that all variables defined are of type Node *, even errptr.

Note that a struct declaration in C++ also defines an implicit typedef — that is, the data type can be referred to as var (rather than struct var) immediately, without any explicit use of the typedef keyword. However, even in C++ it can be worthwhile to define typedefs for more complicated types. For example, a program to manipulate RGB images might find it useful to give the name ImagePointer to the type "pointer to array [3] of int":

typedef int (*ImagePointer) [3] ;

Criticisms

Some people are opposed to the extensive use of typedefs. Most arguments center around the idea that typedefs simply hide the actual data type of a variable. For example, Greg Kroah-Hartman, a Linux kernel hacker and documenter, discourages their use for anything except function prototype declarations. He argues that this practice not only unnecessarily obfuscates code, it can also cause programmers to accidentally misuse large structures thinking them to be simple types [cite web |url=http://www.linuxjournal.com/article/5780 |title=Proper Linux Kernel Coding Style |author=Kroah-Hartman, Greg |authorlink=Greg Kroah-Hartman |date=2002-07-01 |accessdate=2007-09-23 |work=Linux Journal |quote=Using a typedef only hides the real type of a variable.] .

Other languages

In many statically-typed functional languages, like Haskell, Miranda, OCaml, etc., you can define "type synonyms", which are the same as typedefs in C. An example in Haskell:

type pairOfInts = (Int, Int)
We've just defined a type synonym "pairOfInts" which means the same as a pair of Int's.

ee also

*Abstract data type
*C syntax

References

External links

*http://www.cprogramming.com/tutorial/typedef.html - detailed discussion of typedef at "CProgramming.com"


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Typedef — es una palabra reservada en el lenguaje de programación C y C++. Su función es asignar un nombre alternativo a tipos existentes, a menudo cuando su declaración normal es aparatosa, potencialmente confusa o probablemente variable de una… …   Wikipedia Español

  • typedef — noun A statement that defines a data type …   Wiktionary

  • Template method pattern — [ LePUS3 ( [http://lepus.org.uk/ref/legend/legend.xml legend] ) ] In software engineering, the template method pattern is a design pattern.It is a so called behavioral pattern, and is unrelated to C++ templates.IntroductionIn a template pattern,… …   Wikipedia

  • C++0x — is the planned new standard for the C++ programming language. It is intended to replace the existing C++ standard, ISO/IEC 14882, which was published in 1998 and updated in 2003. These predecessors are informally known as C++98 and C++03. The new …   Wikipedia

  • Compound File Binary Format — (CFBF), also called Compound File or Compound Document[1], is a file format for storing numerous files and streams within a single file on a disk. CFBF is developed by Microsoft and is an implementation of Microsoft COM Structured… …   Wikipedia

  • Struktur (Datentyp) — Der Datentyp Struktur (engl. structure) bezeichnet einen Verbund. Durch seine Fähigkeit andere Variablen verschiedener Datentypen zu umfassen, kann eine Struktur zu übersichtlicherem und dynamischem Quelltext verhelfen. Inhaltsverzeichnis 1… …   Deutsch Wikipedia

  • Insight Segmentation and Registration Toolkit — Infobox Software name = ITK caption = ITK Logo developer = Insight Software Consortium latest release version = 3.8.0 latest release date = July 30, 2008 operating system = Cross platform genre = Development Library license =… …   Wikipedia

  • Concepts (C++) — Concepts and the related notion of axioms were an extension to C++ s template system proposed for C++0x. They were designed to improve compiler diagnostics and to allow programmers to codify in the program some formal properties of templates that …   Wikipedia

  • C++11 — C++11, also formerly known as C++0x,[1] is the name of the most recent iteration of the C++ programming language, replacing C++TR1, approved by the ISO as of 12 August 2011.[2] The name is derived from the tradition of naming language versions by …   Wikipedia

  • Multiple dispatch — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”