小甲鱼 P49 typedef---结构体

小甲鱼 P49 typedef

typedef:为类型取别名

typedef 与 结构体:

下面的代码中,使用typedef为结构体Date取个别名为DATE,相当于DATE = struct Date

(我们可以这样看,typedef struct Date{...}  DATE

在给结构体取别名时,同时定义一个指针*PDATE,相当于PDATE=struct Date *

代码如下:

#include <stdio.h>
#include <stdlib.h>

typedef struct Date
{
	int year;
	int month;
	int day;
}DATE, *PDATE;

int main(void)
{
	struct Date *date;
	
	//date = (struct Date *)malloc(sizeof(struct Date));
	date = (PDATE)malloc(sizeof(DATE));
	if (date ==NULL)
	{
		printf("内存分配失败!\n");
		exit(1);
	}
	
	date->year = 2017;
	date->month = 5;
	date->day = 15;
	
	printf("%d-%d-%d\n", date->year, date->month, date->day);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xiaodingqq/article/details/82945142