C Language-keyword typedef

Source: Internet
Author: User

1. Basic use

The use of 1> typedef in basic data types

typedef int MYINT; equals an alias to int.

typedef MYINT MYINT2; It's the equivalent of an alias for MyInt.

MyInt a = 10; Myint equivalent to int

MyInt2 B = 12; MyInt2 equivalent to int

2> typedef and pointers when used together

Char *name = "Jack";

typedef char * string;//equivalent to (char *) a name string

String name1 = "Jame";

3> the use of typedef and structs together

①struct Date {int year; int month; int day};

typedef struct DATE mydate;//is equivalent to an alias for struct date

②typedef truct Date {int year; int month, int day} mydate;//another way to use

③typedef truct {int year; int month, int day} mydate;//another way to use

4> typedef and enumeration when used together

Enum Sex {Sexman,sexwoman,unknow};

typedef enum SEX {Sexman,sexwoman,unknow} mysex;//is equivalent to an alias for an enumeration type

Mysex s = sexman;//defining variables

5> typedef and function pointers when used together

int sum (int a, int b)

{

return a + B;

}//first defines a function

Int (*p) (int, int);//defines a pointer type that points to a function

typedef int (*mypoint) (int, int);//Pointer to function the type of a name MyPoint

MyPoint p = sum;

6> typedef and struct pointers when used together

struct Date {int year; int month; int day};//defines a struct type

struct Date d = {1990, 10, 10};//defines struct-body variables

struct Date *p = &d;//defines a pointer to a struct variable

typedef struct DATE *datepoint;

Datepoint P2 = &d;

typedef struct DATE {int year; int month; int day} * dstepoint;//another way of defining

7> Summary

① function: A new name for an already existing type

② Use occasions:

1) Basic Data type

2) Structural body

3) pointer

4) enumeration

5) Pointer to function

2. Use note

1> typedef and macro definitions are used at the same time, note that the macro definition is text substitution

#denfine string2 char *

Type char * string;

String s1,s2; S1, S2 are all char * types

String2 S3,s4; S3 is a char * type, S4 is a char type, because the macro is a text substitution

C Language-keyword typedef

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.