/typedef
#include"stdio.h"
#include "stdlib.h"
typedef int integer ;
#define integers int // -->直接替换
// #define ptrint int*
// ptrint a,b; --> int *a ,b ;
// but the typedef int *ptrint
// ptrint a,b; --> iny *a ,*b; 两个东西的本质不同
// typedef 相当于类型封装 define 相当于整体代换
struct Data
{
int year;
int month;
int day;
};
typedef struct time
{
int year;
int month;
int day;
}TIME;
int main (void)
{
/*
integer a;
integer b;
a = 520;
b = a;
integers c;
integers d;
c = 500;
d = c;
printf("a = %d\n",a);
printf("b = %d\n",b);
printf("size of a = %d\n",sizeof(a));
printf("a = %d\n",c);
printf("b = %d\n",d);
printf("size of a = %d\n",sizeof(c));
*/
struct Data *data;
data = (struct Data *)malloc(sizeof(struct Data));
if(data == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
data->year = 2017;
data->month = 5;
data->day = 15;
printf("%d-%d-%d\n",data->year,data->month,data->day);
TIME time;
//time = (TIME )malloc(sizeof(TIME));
// if(time == NULL)
// {
// printf("内存分配失败!\n");
// exit(1);
// }
time.year = 2018;
time.month = 7;
time.day = 18;
printf("%d-%d-%d\n",time.year,time.month,time.day);
TIME schedule;
schedule = (TIME *)malloc(sizeof(TIME));
if(schedule == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
schedule->year = 2018;
schedule->month = 7;
schedule->day = 18;
printf("%d-%d-%d\n",schedule->year,schedule->month,schedule->day);
return 0;
}
尝试了多种的tyepdef 的类型替换 , 发现 schedule 不能够
使用MALLOC 函数来分配 大小 ,这钟操作也是很蠢的操作方式,sturct 的类型一生成 就会在内存钟产生内存对齐的空间分配,而malloc函数是针对指针的,它是使指针指向一大块的内存地址。所以上面代码的最下面是十分严重的错误和明显错误的代码,
TIME *schedule;
schedule = (TIME *)malloc(sizeof(TIME));
if(schedule == NULL)
{
printf("内存分配失败!\n");
exit(1);
}
schedule->year = 2018;
schedule->month = 7;
schedule->day = 18;
printf("%d-%d-%d\n",schedule->year,schedule->month,schedule->day);