Namespaces
Variants
Views
Actions

Typedef declaration

From cppreference.com
< c‎ | language
Revision as of 16:50, 16 March 2014 by Newatthis (Talk | contribs)

The typedef declaration provides a way to create a synonym that can be used anywhere in place of a (possibly complex) type name.

Contents

Syntax

typedef type-specifier typedef-name;

Explanation

Arguments for using the synonym from a typedef declaration:

  • parameterizes a C type
  • shortens a lengthy C type name
  • provides a name indicating what a variable represents
  • provides a common C idiom to avoid writing "struct" repeatedly
  • provides a common C idiom to avoid writing "union" repeatedly
  • provides a common C idiom to avoid writing "enum" repeatedly
  • typedef with array
  • typedef with pointer
  • typedef with function pointer
  • can make code clearer
  • can make code easier to maintain

Arguments against using the synonym from a typedef declaration:

  • hides the actual data type of a variable
  • causes accidental misuse of large structures thought to be simple types
  • can make code unnecessarily opaque
  • can make code harder to maintain

Keywords

typedef

Example

Uppercase visually emphasizes the synonyms in the examples. Uppercase is not required.

#include <stdio.h>
 
int main()
{
    /* Parameterizes a C type.                                                   */
    /* When the program requires a larger f-p format, simply change the typedef. */
    typedef float REAL;
    /* Variables x1 and x2 have the same type.                                   */
    REAL x1;
    float x2;
    printf("sizeof(x1) = %zu\n", sizeof(x1));   // 4 bytes
    printf("sizeof(x2) = %zu\n", sizeof(x2));   // 4 bytes
    const REAL y = 0.0;
    static REAL z;
 
//  typedef float REAL;   /* error: redefinition of typedef 'REAL' [-Wpedantic] */
//  typedef double REAL;  /* error: conflicting types for 'REAL'                */
 
    /* Shortens a lengthy C type name. */
    typedef unsigned long int ULONG;
 
    /* Provides a name indicating what a variable represents.              */
    /* Both synonyms are floats but represent different physical concepts. */
    typedef float TEMP;
    typedef float WIND_SPEED;
 
    /* Provides a common C idiom to avoid writing "struct" repeatedly. */
    /* anonymous struct                                                */
    typedef struct {int a; int b;} S, *PS;
    /* Variables ps1 and ps2 have the same type.                       */
    PS ps1;
    S* ps2;
 
    /* Provides a common C idiom to avoid writing "union" repeatedly. */
    /* anonymous union                                                */
    typedef union {int i; float f;} U, *PU;
    /* Variables pu1 and pu2 have the same type.                      */
    PU pu1;
    U* pu2;
 
    /* Provides a common C idiom to avoid writing "enum" repeatedly. */
    /* anonymous enum                                                */
    typedef enum {club,diamond,heart,spade} E, *PE;
    /* Variables pe1 and pe2 have the same type.                     */
    PE pe1;
    E* pe2;
 
    /* typedef with array */
    typedef int ARR_T[10];
    ARR_T a1;
    int a2[10];   /* same as a1 */
    printf("sizeof(a1) = %zu\n", sizeof(a1));   // 40 bytes
    printf("sizeof(a2) = %zu\n", sizeof(a2));   // 40 bytes
 
    /* typedef with pointer */
    int i=0;
    typedef int * PTR_TO_INT;
    PTR_TO_INT pi = &i;
 
    /* typedef with function pointer                                                */
    /* FP is a synonym for a pointer to a function that neither takes nor returns a */
    /* value.                                                                       */
    typedef void (*FP)(void);
 
    /* error: multiple storage classes in declaration specifiers */
    /* typedef static unsigned int uint;                         */
 
    return 0;
}

Possible output:

sizeof(x1) = 4
sizeof(x2) = 4
sizeof(a1) = 40
sizeof(a2) = 40