#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int age;
char name[20];
} nodes;
void Fuc_Print(void* data, int p_len, int len, void (*my_print)(void*)) {
for(int i = 0; i < len; i++) {
my_print((char *)data + i * p_len);
}
}
void print1(void *p_info) {
nodes *p_infos = (nodes *) p_info;
printf("名字:%s\t年龄:%d\n", p_infos -> name, p_infos -> age);
}
void print2(void *p_info) {
int *p_infos = (int *)p_info;
printf("%d ", *p_infos);
}
int main() {
nodes node_array[2] = {
{20, "凯皇"},
{23,"露娜"}
};
printf("---第一次测试---\n");
Fuc_Print(node_array, sizeof(nodes), 2, print1);
int num_s[8] = {1, 2, 3, 4, 5, 6, 7, 8};
printf("---第二次测试---\n");
Fuc_Print(num_s, sizeof(int), 8, print2);
system("pause");
return 0;
}
C语言使用同回调函数——打印不同类型的数据
最新推荐文章于 2024-05-16 21:27:18 发布