指针的用法,函数,结构体,以及部分解释

1.指针函数和函数指针
函数指针 int (*hanshu)(void)
指针函数 int *hanshu (void)

函数指针是一个指针,可以用来指向这种返回值的函数,比如回调函数

typedef 返回类型 (新类型名)(参数列表)
typedef void (
hanshu)(void)

例 用法1

void chihan(void)
{
}
void main (void)
{
hanshu p;//定义函数指针
p = chifan;
p();//可以调用chifan()函数
}

例 用法2 回调

void chihan(hanshu callback)
{
callback();//回调函数
}
void myCallback1(void) {
printf("这是我的回调函数!\n");
}
void myCallback2(void) {
printf("这是另一个回调函数!\n");
}
void main(void)
{
eventHandler(myCallback1);
eventHandler(myCallback2);
}

例 用法3 结构体指针

typedef struct {
int num ;
hanshu init; //函数指针
}ual;
void chifan (void)
{
}
void xishou (void)
{
}
void dushu (void)
{
}
void main (void)
{
ual hound[3]={
{100,chifan},
{200,xishou},
{300,dushu}
};
/函数调用/
hound[0].init();
hound[1].init();
hound[3].init();
}

指针函数是一个函数,返回的是一个指针

指针数组和数组指针

指针数组 int *shuzu[10];

指针数组每个成员都是一个指针

char* even{3}={
"chifan"
"shuijiao"
"dayouxi"
};

数组指针 int (*shuzu)[10];

int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int (ptr)[4] = matrix;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
// 三种等价的访问方式
printf("%2d ", ptr[i][j]);
// printf("%2d ", (
(ptr + i))[j]);
// printf("%2d ", ((ptr + i) + j));
}
printf("\n");
}

posted on 2025-10-27 10:44  脑子疼  阅读(0)  评论(0)    收藏  举报

导航