Introduction to C Language Learning (9) typedef keywords

Source: Internet
Author: User

Introduction to C Language Learning (9) typedef keywords

Keyword: typedef

Usage: define a new name (alias) for various data types)


Typedef and basic data types

Typedef int Integer; Integer a = 8;

You can also create another alias Based on the alias.

Typedef Integer MyInteger; MyInteger a = 8;

The original data type can also be used normally.

Typedef and pointer

Typedef char * String; String str = "jackie ";

Typedef andStruct

TypedefstructPerson Per; // you do not need to include the struct keyword when defining the struct variable.

Per p; p. name = "xyz ";

Define and take the alias:

Typedefstruct Student // The struct name Student can be omitted.

{

Int age;

} Stu;


Void processStudent ()

{

Stu student = {18 };

Student. age = 19;

}

Typedef and pointStructPointer

Typedef struct

{

Int age;

} Stu;

Stu stu = {20 };

Typedef Stu * S; // the alias S for the pointer to the struct.

S s = & stu;


Typedef struct LNode

{

Int data;

Struct LNode * next;

} LinkList, * SList;

Int main (int argc, const char * argv [])

{

LinkList l = {1, NULL };

LinkList ll = {2, NULL };

L. next = & ll;

Printf ("% d,", l. next-> data );

SList sl = & ll;

If (sl-> next! = NULL)

Printf ("% d,", sl-> data );

Return 0;

}


Typedef andEnumeration type

Typedef enum

{

...

} Season;

// Similar to struct

Typedef andPointer to function

Int sum (int a, int B)

{

Return a + B;

}

Void main ()

{

Typedef int (* P) (int a, int B );

P p = sum;

Int result = (* p) (3, 5 );

Return 0;

}

Typedef and # define

Typedef char * String;

String s = "abc"

# Define String char *;

String s = "abc"; // The effect is the same

To use this method:

String s1, s2; // Replace the first one with char * s1, char * s2;

String s3, s4; // replace char * s3, s4; <=> char * s3, char s4;

Related Article

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.