文章目录
- 指针变量类型赋值问题
- 结构体指针
- 指针函数
- use_lcd_by_function_pointer
- typedef与函数指针
指针变量类型赋值问题
int main(int argc, char * argv[]) {
char buf[4]; int i = 0; memset(buf, 1, sizeof(buf)); int *p =(int *)&buf[0]; for (i = 0; i < 4; i ){
printf("buf[%d] == [%#x]\n", i, buf[i]); } printf("------------------------\n"); printf("*p == [%#x]\n", *p); *p = 0x12; printf("------------------------\n"); for (i = 0; i < 4; i ){
printf("buf[%d] == [%#x]\n", i, buf[i]) } return 0; }
打印结果
buf[0] == [0x1]
buf[1] == [0x1]
buf[2] == [0x1]
buf[3] == [0x1]
------------------------
*p == [0x1010101]
------------------------
buf[0] == [0x12]
buf[1] == [0]
buf[2] == [0]
buf[3] == [0]
注意:指针p是int字节,一次操作是对四个字节进行读写数据。
结构体指针
typedef struct student{
char *name;
int age;
struct student * classmate;
int sex;
}stu, * pstu;
定义一个结构体struct,stu代表就是struct student,而pstu是struct * student结构体指针。
指针函数
typedef struct student {
char *name;
int age;
void (*good_work)(void);
struct student * classmate;
}student, * pstudent;
static void play_ball(void)
{
printf("playing ball\r\n");
}
static void sing_song(void)
{
printf("singing\r\n");
}
int main( void )
{
int i;
student ss[2] = {
{
"zhangshan", 10, &play_ball, NULL}, {
"lili", 10, sing_song, NULL}};
prvSetupHardware();
for (i = 0; i < 2; i++)
{
ss[i].good_work();
}
while (1);
return 0;
}
指针函数的定义就是void (*p_func)(void);
关于指针函数的赋值,根据第21行可以看出,关于取值符&
可加,可不加。
use_lcd_by_function_pointer
typedef struct lcd_operation {
int type;
void (*draw_logo)(void);
}lcd_operation, *p_lcd_operation;
int read_eeprom()
{
/* 0: lcd a * 1: lcd b */
return 0;
}
int get_lcd_type(void)
{
return read_eeprom();
}
void draw_logo_lcda(void)
{
printf("display logo on lcd a\r\n");
}
void draw_logo_lcdb(void)
{
printf("display logo on lcd b\r\n");
}
lcd_operation xxx_com_lcds[] = {
{
0, draw_logo_lcda},
{
1, draw_logo_lcdb},
};
p_lcd_operation get_lcd(void)
{
int type = get_lcd_type();
return &xxx_com_lcds[type];
}
int main( void )
{
p_lcd_operation lcd;
lcd = get_lcd();
lcd->draw_logo();
return 0;
}
typedef与函数指针
typedef int (*FP_CALC)(int,int);//定义一个函数指针类型
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参数、
//返回类型为int的函数指针。它的作用是根据操作符返回相应函数的地址
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;
}
//s_calc_func为函数,它的参数是 op,
//返回值为一个拥有两个int参数、返回类型为int的函数指针
int (*s_calc_func(char op)) (int , int)
{
return calc_func(op);
}
//最终用户直接调用的函数,该函数接收两个int整数,
//和一个算术运算符,返回两数的运算结果
int calc(int a, int b, char op)
{
FP_CALC fp = calc_func(op);
int (*s_fp)(int,int) = s_calc_func(op);//用于测试
assert(fp == s_fp);// 可以断言这两个是相等的
if(fp)
return fp(a,b);
else
return -1;
}
void main()
{
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, '/'));
}
FP_CALC calc_func(char op)
和int (*s_calc_func(char op)) (int , int)
,定义一个函数,函数的返回值为函数指针。