在typedef的使用中,最麻烦的是指向函数的指针,如果没有下面的函数,你知道下面这个表达式的定义以及如何使用它吗?
int (*s_calc_func(char op))(int, int);
如果不知道,请看下面的程序,里面有比较详细的说明
// 定义四个函数
int add(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
// 定义指向这类函数的指针
typedef int (*FP_CALC)(int, int);
// 我先不介绍,大家能看懂下一行的内容吗?
int (*s_calc_func(char op))(int, int);
// 下一行的内容与上一行完全相同,
// 定义一个函数calc_func,它根据操作字符 op 返回指向相应的计算函数的指针
FP_CALC calc_func(char op);
// 根据 op 返回相应的计算结果值
int calc(int a, int b, char op);
int add(int a, int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
return b? a/b : -1;
}
// 这个函数的用途与下一个函数作业和调用方式的完全相同,
// 参数为op,而不是最后的两个整形
int (*s_calc_func(char op)) (int, int)
{
return calc_func(op);
}
FP_CALC calc_func(char op)
{
switch (op)
{
case '+': return add;
case '-': return sub;
case '*': return mul;
case '/': return div;
default:
return NULL;
}
return NULL;
}
int calc(int a, int b, char op)
{
FP_CALC fp = calc_func(op); // 下面是类似的直接定义指向函数指针变量
// 下面这行是不用typedef,来实现指向函数的指针的例子,麻烦!
int (*s_fp)(int, int) = s_calc_func(op);
// ASSERT(fp == s_fp); // 可以断言这俩是相等的
if (fp) return fp(a, b);
else return -1;
}
void test_fun()
{
int a = 100, b = 20;
printf("calc(%d, %d, %c) = %d/n", a, b, '+', calc(a, b, '+'));
printf("calc(%d, %d, %c) = %d/n", a, b, '-', calc(a, b, '-'));
printf("calc(%d, %d, %c) = %d/n", a, b, '*', calc(a, b, '*'));
printf("calc(%d, %d, %c) = %d/n", a, b, '/', calc(a, b, '/'));
}
运行结果
calc(100, 20, +) = 120
calc(100, 20, -) = 80
calc(100, 20, *) = 2000
calc(100, 20, /) = 5
http://dev.csdn.net/article/18/18721.shtm
===============================================
typedef 使用大全2(结构体)
续 数组
#define
S(s) printf("%s/n", #s); s
typedef
struct _TS1{
int x, y;
} TS1, *PTS1, ***PPPTS1; // TS1
是结构体的名称,
PTS1
是结构体指针的名称
//
也就是将结构体
struct _TS1
命名为
TS1,
//
将
struct _TS1 *
命名为
PTS1
//
将
struct _TS1 ***
命名为
PPPTS1
typedef
struct { // struct
后面的结构体说明也可以去掉
int x, y;
} TS2, *PTS2;
typedef
PTS1 *PPTS1; //
定义
PPTS1
是指向
PTS1
的指针
typedef
struct _TTS1{
typedef struct ITTS1 {
int x, y;
} iner;
iner i;
int x, y;
} TTS1;
//
结构体内部的结构体也一样可以定义
typedef
TTS1::ITTS1 ITS1;
void
test_struct()
{
//
基本结构体重定义的使用
TS1 ts1 = {100, 200};
PTS1 pts1 = &ts1; //
完全等价于
TS1* pts1 = &ts1;
PPTS1 ppts1 = &pts1; //
完全等价于
TS1** ppts1 = &pts1;
PPPTS1 pppts1 = &ppts1; //
完全等价于
TS1*** pppts1 = &ppts1;
TS2 ts2 = {99, 88};
PTS2 pts2 = &ts2; //
完全等价于
TS2* pts2 = &ts2;
TTS1 itts1 = {{110, 220}, 10, 20};
Its1* rits1 = &itts1.i;
ITS1* &its1 = rits1; //
等价于
TTS1::ITTS1 *its1 = &(itts1.i);
printf("ts1/t = (%d, %d)/n*pts1/t = (%d, %d)/n"
"**ppts1/t = (%d, %d)/n***pppts1= (%d, %d)/n/n",
ts1.x, ts1.y, pts1->x, pts1->y,
(**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);
printf("ts2/t = (%d, %d)/n*pts2/t = (%d, %d)/n/n",
ts2.x, ts2.y, pts2->x, pts2->y);
printf("itts1/t = [(%d, %d), %d, %d]/n*its1/t = (%d, %d)/n/n",
itts1.i.x, itts1.i.y, itts1.x, itts1.y, its1->x, its1->y);
S(pts1->x = 119);
S(pts2->y = 911);
S(its1->x = 999);
printf("ts1/t = (%d, %d)/n*pts1/t = (%d, %d)/n"
"**ppts1/t = (%d, %d)/n***pppts1= (%d, %d)/n/n",
ts1.x, ts1.y, pts1->x, pts1->y,
(**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);
printf("ts2/t = (%d, %d)/n*pts2/t = (%d, %d)/n/n",
ts2.x, ts2.y, pts2->x, pts2->y);
printf("itts1/t = [(%d, %d), %d, %d]/n*its1/t = (%d, %d)/n/n",
itts1.i.x, itts1.i.y, itts1.x, itts1.y, its1->x, its1->y);
S((*ppts1)->y = -9999);
printf("ts1/t = (%d, %d)/n**ppts1/t = (%d, %d)/n/n",
ts1.x, ts1.y, (*ppts1)->x, (*ppts1)->y);
S((**pppts1)->x = -12345);
S((***pppts1).y = -67890);
printf("ts1/t = (%d, %d)/n*pts1/t = (%d, %d)/n"
"**ppts1/t = (%d, %d)/n***pppts1= (%d, %d)/n/n",
ts1.x, ts1.y, pts1->x, pts1->y,
(**ppts1).x, (**ppts1).y, (***pppts1).x, (***pppts1).y);
}
==============
DWORD是双字类型 4个字节,API函数中有很多参数和返回值是DWORD的
相关的有 WORD BYTE 等
相关的有 WORD BYTE 等