Typedef
typedef is in the programming language C and C ++ , a keyword which is used to create an alias for a data type is used.
A simple example where a new type is set for integer speeds, identical to the predefined type int.
typedef int km_pro_Stunde;
Typdef is more often used for more complicated definitions: It is possible and common to define further derivatives of derived types:
int array[10][20]; // Array mit 10 Elementen, wobei jedes Element ein Array von 20 »int« ist.
int *p [10]; // Array mit 10 Elementen, wobei jedes Element ein »int *« (=Zeiger auf »int«) ist.
int (*q)[10]; // Zeiger auf ein Array mit 10 »int«-Elementen.
int (*f)(int *); // Zeiger auf eine Funktion, die einen »int *«-Parameter hat und ein »int« zurückgibt
This can quickly lead to complex and confusing declarations. A typedef declaration, it is possible to assign a (possibly complex) data type a simple name:
void *(*get_cb(int))(void *); // Unübersichtlich!
// Besser:
typedef void *(*callback)(void *); // Typedef: »callback« ist ein Zeiger auf eine Funktion,
// die einen »void*«-Parameter hat und ein »void*« zurückgibt
callback get_cb(int); // Übersichtlicher: »get_cb« hat einen »int«-Parameter und gibt einen
// »callback«-Funktionszeiger zurück
literature
- Brian Kernighan, Dennis Ritchie: The C Programming Language . 2nd edition, Prentice Hall, Englewood Cliffs (NJ) 1988, ISBN 0-13-110362-8 , pp. 146-147. (German translation: Brian Kernighan, Dennis Ritchie: Programming in C. With the C-Reference Manual in German . 2nd edition, Hanser, Munich / Vienna 1990, ISBN 978-3-446-15497-1 )
- Robert climate, Siegfried Selberherr : Programming in C . 3rd edition, Springer, Vienna 2010, ISBN 978-3-7091-0392-0 , pp. 194-195.
- Jürgen Wolf: C from A to Z. The comprehensive manual . 3rd updated and expanded edition 2009, 4th, corrected reprint 2015, Rheinwerk, Bonn 2015, ISBN 978-3-8362-1411-7 .