Introduction to C language Learning (eight) typedef keyword

Source: Internet
Author: User


Key Words: 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 alias an alias based on the

typedef Integer Myinteger; Myinteger AA = 8;

The original data type can also be used normally

typedef with the pointer

typedef Char *String; String str = "stone";

typedef with the structural body

typedefstruct person Per; // This will not bring up the struct keyword when defining struct variables.

Per p; P.name = "xyz";

Define and take aliases:

typedef struct Student//struct name Student can be omitted

{

int age;

} Stu;


void processstudent ()

{

Stu student = {+};

Student. age =+;

}

typedef and Point Structural Body the pointer

typedef struct

{

int age;

} Stu;

Stu Stu = {20};

typedef STU *S; //Pointer to struct to take alias S

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 = ≪

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

SList SL = ≪

if (sl->next! = NULL)

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

return 0;

}


typedef with enum types

typedef enum

{

...

} Season;

Usage is similar to structural body

typedef with the pointers to functions

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 with the #define

typedef char *string;

String s = "abc"

#define String char *;

String s = "abc"; This uses the same effect

When this is used:

String s1,s2; //With the first substitution: Char *s1, char *s2;

String S3,s4;   //With second substitution: char * S3, S4; <==> Char *s3, Char S4;

Introduction to C language Learning (eight) typedef keyword

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.