码迷,mamicode.com
首页 > 编程语言 > 详细

C语言学习入门 (八) typedef 关键字

时间:2015-01-08 13:25:06      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:c语言   typedef   


关键字:typedef

用法:为各种数据类型定义一个新名字(别名)


typedef与基本数据类型

typedef int Integer;Integer a 8

也可以在别名的基础上再起一个别名

typedef IntegerMyInteger;MyInteger aa = 8;

原来的数据类型也可以正常使用

typedef与指针

typedef char *String;String str = “stone”;

typedef结构体

typedefstructPerson Per;// 这样在定义结构体变量时 就不用带上struct 关键字了

Per p; p.name = “xyz”;

定义并取别名:

typedefstruct Student// 结构体名 Student 可以省略

{

   int age;

    

} Stu;


void processStudent()

{

   Stu student = {18};

    student.age =19;

}

typedef与指向结构体的指针

typedef struct 

{

int age;

} Stu;

Stu stu = {20};

typedef Stu *S; //指向结构体的指针  取别名 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枚举类型

typedef enum 

{

} Season;  

//用法与结构体类似

typedef指向函数的指针

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

typedef char *String;

String s = “abc”

#define String char *;

String s = “abc”;  //这样使用效果一样

当 这样使用:

String s1,s2; //用第一种替换: char *s1, char *s2;

String s3,s4; //用第二种替换: char * s3, s4;   <==>  char *s3, char s4;

C语言学习入门 (八) typedef 关键字

标签:c语言   typedef   

原文地址:http://blog.csdn.net/jjwwmlp456/article/details/42522445

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!