Common · Knowledge
Typedef
Reserved keyword in the C and C++ programming languages
typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type.
From Wikipedia
typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, although it is also commonly used to provide specific descriptive type names for integer data types of varying sizes. A typedef declaration follows the same syntax as declaring any other C identifier. The keyword typedef itself is a specifier which means that while it typically appears at the start of the declaration, it can also appear after the type specifiers or between two of them. In the C standard library and in POSIX specifications, the identifier for the typedef definition is often suffixed with _t, such as in size_t and time_t. This is practiced in some other coding systems, although POSIX explicitly reserves this practice for POSIX data types. In C++, this _t suffix is not used for typedefs (as C++ does not mandate prefixing structs with the struct keyword), but does appear in some types in the C++ Standard Library (especially for tag types, like std::in_place_t) along with other suffixes like _v (typically representing an abbreviation for a T::value field). This snippet declares the type Length as an alias of the type int, allowing a user to use Length as a type in declarations where they would otherwise use int: A typedef declaration may be used to give more contextual meaning than is given by the underlying type, hinting to the user of functions or variables a certain meaning or format is expected. For example, in the code below, the type int is being further clarified to the distinct aliases Points and KmPerHour: The use of typedef here is used to indicate that only variables declared as Points (or variables that can be interpreted as a Points type) should be used as the first argument, and variables declared as KmPerHour for the second.
Text: Wikipédia, CC BY-SA 4.0. ·