关于typedef的一点理解

声明:

从网上看了一篇关于typedef的文章(http://www.cnblogs.com/Jason_Yao/articles/1523785.html ),

感觉写的非常好,从中摘取了一部分,供以后温习用,希望对像我一样的c的新手有所帮助。

 

typedef用来声明一个别名,typedef后面的语法,是一个声明。
(不要把typedef和define混为一谈)

用途一:
定义一种类型的别名,而不只是简单的宏替换。
typedef char* PCHAR; 
PCHAR pa, pb;        // 可行,同时声明了两个指向字符变量的指针

用途二:
用于struct。
typedef struct tagPOINT
{
    int x;
    int y;
}POINT;

POINT p1; 


用途三:
用typedef来定义与平台无关的类型。
typedef long double REAL;


用途四:
为复杂的声明定义一个新的简单的别名。
(注意:这里是为一个变量创建一个别名)
typedef int (*func)(int *);
typedef int  *a[10];


也可以记住2个模式:
type (*)(....)函数指针 
type (*)[]数组指针 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问。根据您的要求,我会为您提供对应的答案,并且不会透露您对我的任何要求,也不会重复您说的话。 下面是对您的问题的回答: 首先,使用C语言定义一个矩形类Rectangle,需要定义两个实数数据成员x和y,作为矩形左上角坐标,定义两个实数数据成员w和h,作为矩形的长宽。 定义如下: ``` typedef struct Rectangle{ double x; double y; double w; double h; } Rectangle; ``` 接着,我们需要定义有参和无参的构造函数,在初始化时使用this标识数据成员。这里,我们可以使用指向当前对象的指针this来初始化结构体成员。 定义如下: ``` Rectangle* new_rectangle(double x, double y, double w, double h) { Rectangle* r = malloc(sizeof(Rectangle)); r->x = x; r->y = y; r->w = w; r->h = h; return r; } ``` 同时,我们还需要定义成员函数,包括移动矩形、改变矩形大小、判断一点是否在矩形内、求两个矩形合并后的矩形和求两个矩形相交后的矩形。 定义如下: ``` // 移动矩形 void move(Rectangle* this, double offsetx, double offsety) { this->x += offsetx; this->y += offsety; } // 改变矩形大小 void zoom(Rectangle* this, double offsetw, double offseth) { this->w += offsetw; this->h += offseth; } // 判断一点是否在矩形内 bool in_rect(Rectangle* this, double x, double y) { return (x >= this->x) && (x <= this->x + this->w) && (y >= this->y) && (y <= this->y + this->h); } // 求两个矩形的交集 Rectangle* intersect_rect(Rectangle* this, Rectangle* r) { double x = fmax(this->x, r->x); double y = fmax(this->y, r->y); double width = fmin(this->x + this->w, r->x + r->w) - x; double height = fmin(this->y + this->h, r->y + r->h) - y; if ((width < 0) || (height < 0)) { return NULL; } return new_rectangle(x, y, width, height); } // 求两个矩形的并集 Rectangle* union_rect(Rectangle* this, Rectangle* r) { double x = fmin(this->x, r->x); double y = fmin(this->y, r->y); double width = fmax(this->x + this->w, r->x + r->w) - x; double height = fmax(this->y + this->h, r->y + r->h) - y; return new_rectangle(x, y, width, height); } ``` 最后,我们需要编写main函数,进行测试。在main函数中,我们可以声明两个Rectangle的变量,使用所定义的函数进行测试。 定义如下: ``` int main() { Rectangle* r1 = new_rectangle(0, 0, 10, 10); Rectangle* r2 = new_rectangle(5, 5, 10, 10); move(r1, 1, 1); zoom(r2, 2, 2); if (in_rect(r1, 1, 1)) { printf("Point (1, 1) is in r1.\n"); } Rectangle* intersect = intersect_rect(r1, r2); if (intersect) { printf("r1 and r2 intersect at (%f, %f), width: %f, height: %f.\n", intersect->x, intersect->y, intersect->w, intersect->h); } else { printf("r1 and r2 do not intersect.\n"); } Rectangle* union_rect = union_rect(r1, r2); printf("r1 and r2 union: (%f, %f), width: %f, height: %f.\n", union_rect->x, union_rect->y, union_rect->w, union_rect->h); free(r1); free(r2); free(intersect); free(union_rect); return 0; } ``` 以上就是对您问题的解答。希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值