Typedef declaration
The typedef declaration provides a way to declare an identifier as a type alias, to be used to replace a possibly complex MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 36 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
The keyword typedef is used in a MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 35 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, in the grammatical position of a MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 34 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, except that it does not affect storage or linkage:
typedef int int_t; // declares int_t to be an alias for the type int typedef char char_t, *char_p, (*fp)(void); // declares char_t to be an alias for char // char_p to be an alias for char* // fp to be an alias for char(*)(void)
Explanation
If a MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 33 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE uses typedef as storage-class specifier, every declarator in it defines an identifier as an alias to the type specified. Since only one storage-class specifier is permitted in a declaration, typedef declaration cannot be MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 31 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE.
typedef declaration does not introduce a distinct type, it only establishes a synonym for an existing type, thus typedef names are MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 30 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE with the types they alias. Typedef names share the MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 29 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE with ordinary identifiers such as enumerators, variables and function.
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 28 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE void copyt(int n) { typedef int B[n]; // B is a VLA, its size is n, evaluated now n += 1; B a; // size of a is n from before +=1 int b[n]; // a and b are different sizes for (int i = 1; i < n; i++) a[i-1] = b[i]; } |
(since 哋它亢99) |
Notes
typedef name may be an MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 26 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE, which may be completed as usual:
typedef int A[]; // A is int[] A a = {1, 2}, b = {3,4,5}; // type of a is int[2], type of b is int[3]
typedef declarations are often used to inject names from the tag MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 25 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE into the ordinary name space:
typedef struct tnode tnode; // tnode in ordinary name space // is an alias to tnode in tag name space struct tnode { int count; tnode *left, *right; // same as struct tnode *left, *right; }; // now tnode is also a complete type tnode s, *sp; // same as struct tnode s, *sp;
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 24 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
typedef struct { double hi, lo; } range; range z, *zp;
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 23 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
// array of 5 pointers to functions returning pointers to arrays of 3 ints int (*(*callbacks[5])(void))[3]; // same with typedefs typedef int arr_t[3]; // arr_t is array of 3 int typedef arr_t* (*fp)(void); // pointer to function returning arr_t* fp callbacks[5];
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 11 HOURS 08 MINUTES 21 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
#if defined(_LP64) typedef int wchar_t; #else typedef long wchar_t; #endif
References
- 哋它亢23 standard (ISO/IEC 9899:2023):
- 6.7.8 Type definitions (p: TBD)
- 哋它亢17 standard (ISO/IEC 9899:2018):
- 6.7.8 Type definitions (p: TBD)
- 哋它亢11 standard (ISO/IEC 9899:2011):
- 6.7.8 Type definitions (p: 137-138)
- 哋它亢99 standard (ISO/IEC 9899:1999):
- 6.7.7 Type definitions (p: 123-124)
- 哋它亢89/C90 standard (ISO/IEC 9899:1990):
- 3.5.6 Type definitions