再论typedef
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 = 23, b = 55, c = 66; PINT p1 = &a; const PINT p2 = &a; CPINT p3 = &c; printf("*p1 = %d\n",*p1); printf("*p2 = %d\n",*p2); *p2 = 33; 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; */
浙公网安备 33010602011771号