C语言学习笔记————–typedef

/ 0评 / 0

typedef

        typedef 是C语言中很重要的一个关键字,就是给类型取别名,像基本数据类型、指针、结构体、枚举等都可以使用 typedef 去取别名,比如

 
#include 

typedef int DCInteger;

typedef char *String;

struct People {
    char *name; // 姓名
    int age; // 年龄
    float height; // 身高
};

typedef struct People Person;

void testTypedef(){
	DCInteger a = 10;

	printf("%d\n", a);

	String str = "这是一个字符串!";

	printf("%s\n", str);

	Person p;

	p.age = 10;
}

//其实我们在定义结构体的时候就可以顺便给结构体起一个别名
typedef struct People{
    char *name; // 姓名
    int age; // 年龄
    float height; // 身高
} Person;

//甚至可以省略掉结构体名
typedef struct{
    char *name; // 姓名
    int age; // 年龄
    float height; // 身高
} Person;

int main(int argc, char const *argv[]){
	testTypedef();

	return 0;
}

评论已关闭。