The role of typedef is to name the known data type aliases, and play the following roles;
1 Simplify complex data type names
2 Use typedef to define platform-independent data types
3 Enhance code readability
4 Avoid errors
An example program using typedef is as follows;
// tydemo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
typedef struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} mybook;
int main(int argc, char* argv[])
{
mybook book;
strcpy( book.title, "AAA");
strcpy( book.author, "CCCCC");
strcpy( book.subject, "FFFFFFFFFFFF");
book.book_id = 12345;
printf( "书标题 : %s\n", book.title);
printf( "书作者 : %s\n", book.author);
printf( "书类目 : %s\n", book.subject);
printf( "书 ID : %d\n", book.book_id);
getchar();
return 0;
}
Windows uses typedef or #define to define a lot of new data types, in the windows.h header file:
typedef int INT; /* integer*/
typedef unsigned int UINT; /* unsigned integer*/
typedef unsigned int *PUINT; /* unsigned integer pointer*/
typedef int BOOL; /* Boolean type*/
typedef unsigned char BYTE; /* Byte*/
typedef unsigned short WORD; /* WORD (unsigned short) */
typedef unsigned long DWORD; /* DOUBLE WORD (unsigned long)*/
typedef float FLOAT; /* floating point* /
typedef FLOAT *PFLOAT; /* Pointer to float type*/
typedef BOOL near *PBOOL; /* Pointer to Boolean type*/
typedef BOOL far *LPBOOL;
typedef BYTE near *PBYTE; /* Pointer to byte type*/
typedef BYTE far *LPBYTE;
typedef int near *PINT; /* integer pointer*/
typedef int far *LPINT;
typedef WORD near *PWORD; /* pointer to WORD type*/
typedef WORD far *LPWORD;
typedef long far *LPLONG; /* pointer to long integer Pointer*/
typedef DWORD near *PDWORD; /* pointer to DWORD type*/
typedef DWORD far *LPDWORD;
typedef void far *LPVOID; /* pointer to void type*/
typedef CONST void far *LPCVOID; /* pointer Constant pointer of void type*/