前言
在我们学习开发中, 有很多类型都是系统自带的, 比如int, double, long, 指针类型, 如果, 你是系统自带类型恐惧症患者, 那么typedef就可以帮到你了, 请仔细看下面的内容.
开始
首先让我们来看一个例子:
#include <stdio.h>
typedef int MyInt;
int main()
{
int a = 10;
MyInt b = a;
printf("a = %d b = %d\n", a, b);
return 0;
}
输出的结果:
有些人会奇怪, 为什么会这样子呢??? 下面我们来详细说明一下:
其实使用typedef 定义的类型名, 就相当于给系统类型起一个别名, 我使用了这个别名, 就相当于使用了系统类型, 比如, 你家人给你起了一个别名是仔仔或者女女, 他们叫仔仔或者女女就相当于叫你们一样, typedef就是这样的一个作用.
typedf可以给系统所有的类型起别名, 比如:
#include <stdio.h>
typedef int MyInt;
typedef char * String;
typedef double xiaoshu;
int main()
{
int a = 10;
MyInt b = a;
String c = "abcd";
xiaoshu d = 10.5;
printf("a = %d b = %d c = %s d = %f\n", a, b, c, d);
return 0;
}
输出的结果:
typedef的好处远远不止这些, 下面我们继续来看:
#include <stdio.h>
struct Student
{
int age;
};
typedef struct Student Stu;
int main()
{
struct Student stu = {20};
Stu stu2 = {10};
printf("stu = %d stu2 = %d\n", stu.age, stu2.age);
return 0;
}
输出结果:
使用了typedef 给结构体起别名, 可以更好的精简代码, 减少一定的工作时间, 提高效率~~还有两种方法, 下次我们继续来看看:
1.
#include <stdio.h>
typedef struct Student
{
int age;
}MyStu;
int main()
{
struct Student stu = {20};
MyStu stu1 = {10};
printf("stu = %d stu1 = %d\n", stu.age, stu1.age);
return 0;
}
2.
#include <stdio.h>
typedef struct
{
int age;
}Student;
int main()
{
Student stu = {10};
printf("stu = %d\n", stu.age);
return 0;
}
之前, 我们在结构体的章节里面说过, 定义结构体也可以不用定义它的类型名, 使用typedef也同样如此, 那么这样做的好处和坏处又是怎么样呢?下面我们来看看
1. 好处:
使用第二种方法, 我们就可以使用更加正规的名称, 减少代码量, 提高效率.
2. 坏处:
使用第二种方法没有第一种方法那么灵活, 第一种使用了typedef之后仍然可以使用原来的方法初始化结构体, 但是第二种就不可以了.
同样typedef一样可以运用在枚举类型上:
1.
#include <stdio.h>
enum Human
{
man,
women
};
typedef enum Human people;
int main()
{
people s = man;
people s1 = women;
return 0;
}
#include <stdio.h>
typedef enum Human
{
man,
women
}people;
int main()
{
people s = man;
people s1 = women;
return 0;
}
#include <stdio.h>
typedef enum
{
man,
women
}people;
int main()
{
people s = man;
people s1 = women;
return 0;
}
好处和坏处与上面的结构体一样, 这里就不多解释啦~~~
但如果把typedf运用在指针上, 就有所不同, 下面我们来看看, 比如:
#include <stdio.h>
int sum(int a, int b)
{
return a+b;
}
int main()
{
int (*p)(int, int) = sum;
int c = p(10, 11);
printf("%d\n", c);
return 0;
}
#include <stdio.h>
typedef int (*MyAddress)(int, int);
int sum(int a, int b)
{
return a+b;
}
int main()
{
int (*p)(int, int) = sum;
int a = p(10, 11);
MyAddress address = sum;
int b = address(10, 20);
printf("a = %d b = %d\n", a, b);
return 0;
}
在指针里面, 我们要自定义的名称是写在*号后面, 而不是像之前那样, 放在后面的了, 这个需要注意一下
还有当指针, 结构体还有typedef结合又是怎么样的呢? 我们继续往下看:
#include <stdio.h>
struct Person
{
int age;
};
int main()
{
struct Person p = {20};
struct Person *p1 = &p;
return 0;
}
在我们没有接触typedef的时候, 我们需要这样子去定义, 但我接触了typedef之后就可以这样子书写~~
#include <stdio.h>
typedef struct Person
{
int age;
}MyPerson;
typedef struct Person * PersonPoint;
int main()
{
MyPerson p = {20};
PersonPoint p2 = &p;
return 0;
}
这样子, 我们就可以非常简单的实现指针指向结构体啦, 不需要再写一大堆代码~~
当然还有另外一种写法~~~比如:
#include <stdio.h>
typedef struct Person
{
int age;
} * PersonPoint;
int main()
{
struct Person p = {20};
PersonPoint p2 = &p;
printf("p = %d p2 = %d\n", p.age, p2->age);
return 0;
}
当然, 想怎么用关键还是取决于你, 喜欢用什么方式就用什么方式~~~
好啦, 这次就讲到这里, 下次我们继续~~~