再论typedef
2024-08-24 19:05:33
typedef 定义(或者叫重命名)类型而不是变量
1、类型是一个数据模板,变量是一个实在的数据。类型是不占内存的,而变量是占内存的。
2、面向对象的语言中:类型的类class,变量就是对象。
#include<stdio.h> //结构体类型的定义
// struct student
// {
// char name[20];
// int age;
// }; //定义一个结构体类型,这个类型有2个名
typedef struct student
{
char name[20];
int age;
} student; //第一个结构体类型 struct teacher, teacher;
//第二个结构体指针类型struct teacher *,pTeacher;
typedef struct teacher1
{
char name[20];
int age;
int mager;
}teacher, *pTeacher; int main(void)
{
student St1,St2;
teacher Tc1,Tc2; pTeacher Tc3,Tc4; Tc1.age = 11;
Tc3 = &Tc1; St1.age = 12;
printf("%d\n",Tc3->age);
}
typedef 加const的用法
#include<stdio.h> typedef int *PINT;
typedef const int *CPINT;
//const int *p 和 int *const p是不同的。前者是p指向的变量是const,后者是p本身const int main(void)
{
int a = , b = , c = ;
PINT p1 = &a; const PINT p2 = &a;
CPINT p3 = &c; printf("*p1 = %d\n",*p1);
printf("*p2 = %d\n",*p2); *p2 = ;
printf("*p2 = %d\n",*p2); //*p3 = 88; //此语句出错,
p3 = &b;
printf("*p3 = %d\n",*p3); // PINT p2 = &b; //此语句出错
// printf("*p2 = %d\n",*p2);
}
/*
typedef int *PINT; const PINT p2;相当于int *const p2;
typedef int *PINT; PINT const p2;相当于int *const p2;
如果确实想得到const int *p;这种效果,只能typedef const int *CPINT;CPINT p1;
*/
最新文章
- Spring学习记录(三)---bean自动装配autowire
- Debugging into .NET Core源代码的两种方式
- 首师大附中互测题:99999999海岛帝国后传:算法大会【D001】
- android上引入七牛 上传图片或者文件 最终整理版本(可用)
- VS2010 asp.net development server 无法展示svg图片
- jquery selector checkbox
- Fiddler工具的基本功能
- openlayers加载百度地图
- Torry的困惑(基本型)
- 搜索(DLX重复覆盖模板):HDU 2295 Radar
- ListView中RadioButton实现单项选择
- 怎么写cookie
- .net使用RabbitMQ
- 使用open-falcon监控Nginx
- bzoj4361isn 容斥+bit优化dp
- HeadFirst设计模式读书笔记之策略模式
- SQLServer之创建非聚集索引
- 创建线程时如果既传入了runnable对象,又继承thread重写了run方法,会执行的哪里的代码
- js 函数中的this
- 测试那些事儿—postman进阶使用与实战