typedef与#define区别
typedef仅限于数据类型,而不是表达式或具体的值
#define是发生在预处理,typedef发生在编译阶段
#define常量起别名(简单的文字替代),typedef是给类型起别名(满足连续定义)
#include <stdio.h>
int main()
{
int i,j;
i=100;
j=200;
printf("%d %d\n",i,j);
typedef int i_new;
i_new a,b;
a=300;
b=400;
printf("%d %d\n",a,b);
return 0;
}
结果
100 200
300 400
--------------------------------
Process exited after 0.6061 seconds with return value 0
请按任意键继续. . .
结构体指针
定义方法
#include <stdio.h>
void test(void)
{
struct stu//声明一个结构体类型
{
int id;
char name[20];
float score;
}stu01={1001,"超级赛亚人",4236.5};;
//定义一个结构体指针保存变量地址
struct stu *p=NULL;//定义时建议指向null
p=&stu01;//让结构体指针变量指向结构体变量stu01
printf("id=%d name=%s score=%.2f \n",(*p).id,(*p).name,(*p).score);//.的优先级\
比*高,所以加括号
printf("\n");
printf("id=%d name=%s score=%.2f \n",stu01.id,stu01.name,stu01.score);
printf("\n");
printf("id=%d name=%s score=%.2f \n",p->id,p->name,p->score);//指针变量可用 推荐
printf("\n");
}
int main()
{
typedef struct//声明一个结构体类型
{
int id;
char name[20];
float score;
}Stu;
//用新名称定义变量
Stu stu01={1001,"超级赛亚人",4236.5};
//定义一个结构体指针保存变量地址
Stu *p=NULL;//定义时建议指向null
p=&stu01;//让结构体指针变量指向结构体变量stu01
printf("id=%d name=%s score=%.2f \n",(*p).id,(*p).name,(*p).score);//.的优先级\
比*高,所以加括号
printf("\n");
printf("id=%d name=%s score=%.2f \n",stu01.id,stu01.name,stu01.score);
printf("\n");
printf("id=%d name=%s score=%.2f \n",p->id,p->name,p->score);//指针变量可用 推荐
printf("\n");
test();
return 0;
}
结果
id=1001 name=超级赛亚人 score=4236.50
id=1001 name=超级赛亚人 score=4236.50
id=1001 name=超级赛亚人 score=4236.50
id=1001 name=超级赛亚人 score=4236.50
id=1001 name=超级赛亚人 score=4236.50
id=1001 name=超级赛亚人 score=4236.50
--------------------------------
Process exited after 0.579 seconds with return value 0
请按任意键继续. . .
结构体指针嵌套
#include <stdio.h>
int main()
{
typedef struct
{
int year;
int month;
int day;
}Birth;
struct stu
{
int id;
char name[20];
Birth *bir_1;
};
struct stu stu01={1001,"小脑斧"};
Birth bir_0={1999,4,26};
stu01.bir_1=&bir_0;
// stu01.bir_1->year=1999;
// stu01.bir_1->month=4;
// stu01.bir_1->day=26;
printf("%d %s %d-%d-%d\n",stu01.id,stu01.name,stu01.bir_1->year,\
stu01.bir_1->month,stu01.bir_1->day);
}
结果
1001 小脑斧 1999-4-26
--------------------------------
Process exited after 0.5051 seconds with return value 22
请按任意键继续. . .