Typedef

Typedef

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 implementación a otra.

Contenido

Ejemplos de uso

Ejemplo básico de typedef

Considere este código:

  #include<stdio.h>
  int main() 
  { 
     int notas; 
     notas=100; 
  }


Ahora considere esto :

  #include<stdio.h>
int main() { typedef int nota_alumno_t; nota_alumno_t notas; notas=100; }


Ambas secciones de código hacen lo mismo: crean un tipo int (notas) y le dan un valor de 100. El método para hacer esto en la segunda sección hace que sea más legible porque la declaración typedef hace que nota_alumno_t signifique lo mismo que int. En este ejemplo, la variable notas guarda las "notas" de un estudiante, así que definir notas como una variable de tipo nota_alumno_t le da al nombre de esa variable un contexto.

Ejemplo con struct

Un ejemplo más:

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

Aquí, un tipo var ha sido definido por el usuario. Así que para crea una variable de tipo var, usamos este código:

  struct var a; 


Agreguemos esta línea:

  typedef struct var nuevotipo;

Ahora, para crear una variable de tipo var, alcanza con:

  nuevotipo a;

Esto es más legible porque no se requiere la palabra reservada struct antes de cada variable de tipo var.

Enlaces externos


Wikimedia foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Mira otros diccionarios:

  • 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 examplesConsider this code:int coxes;int jaffa;...coxes++;...if (jaffa …   Wikipedia

  • 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

Compartir el artículo y extractos

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