#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
typedef struct {
int radius;
Point center;
} Circle;
typedef struct {
Point start;
Point end;
} Line;
typedef enum {
CIRCLE_debug,
LINE,
} ShapeType;
typedef struct {
ShapeType type;/*作为判断条件*/
union {/*关联结构体Circle和Line*/
Circle circle;
Line line;
} shape;//联合体,同一内存能分别存储Circle和Line但不能同时存在。
} Shape;//结构体
void move(Shape* shape, int dx, int dy) {
if (shape == NULL) {
return;
}
if (shape->type == CIRCLE_debug/*都是以CIRCLE_debug作为判断条件和调用关联*/) {
shape->shape.circle.center.x += dx;
shape->shape.circle.center.y += dy;
} else if (shape->type == LINE) {
shape->shape.line.start.x += dx;
shape->shape.line.start.y += dy;
shape->shape.line.end.x += dx;
shape->shape.line.end.y += dy;
}
};
int main() {
// 初始化:创建一个圆和一条线
Shape circleShape = { CIRCLE_debug, { .circle = { 5, { 0, 0 } } } };//.为指定初始化器
Shape lineShape = { LINE, { .line = { { 0, 0 }, { 5, 5 } } } };
// 移动圆和线
move(&circleShape, 2, 2);
move(&lineShape, 2, 2);
// 输出移动后的圆和线
printf("Circle: (%d, %d), radius = %d\n", circleShape.shape.circle.center.x, circleShape.shape.circle.center.y, circleShape.shape.circle.radius);
printf("Line: (%d, %d) - (%d, %d)\n", lineShape.shape.line.start.x, lineShape.shape.line.start.y, lineShape.shape.line.end.x, lineShape.shape.line.end.y);
return 0;
}
定义了一个名为 Shape 的结构,它可以存储 Circle 或 Line。 Circle 结构存储圆的半径和圆心,Line 结构存储直线的起点和终点。
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
typedef struct {
int radius;
Point center;
int type; // 添加类型字段
} Circle;
typedef struct {
Point start;
Point end;
int type; // 添加类型字段
} Line;
//指针函数首先是一个函数,调用方式如同普通函数,传递参数是指针,调用的时候用地址。
void move(void* obj, int dx, int dy) {//void *obj对应Circle*)obj,(Line*)obj
if (obj == NULL) {
return;
}
if (((Circle*)obj)->type == 1) { // 圆
Circle* c = (Circle*)obj;
c->center.x += dx;
c->center.y += dy;
} else if (((Line*)obj)->type == 2) { // 线
Line* l = (Line*)obj;
l->start.x += dx;
l->start.y += dy;
l->end.x += dx;
l->end.y += dy;
}
}
int main() {
// 创建一个圆和一条线
Circle c = { 5, { 0, 0 }, 1 };//初始化数据类型与struct对应上。type为1与move对应。
Line l = { { 0, 0 }, { 5, 5 }, 2 };
// 移动圆和线
move(&c, 2, 2);
move(&l, 2, 2);
// 输出移动后的圆和线
printf("Circle: (%d, %d), radius = %d\n", c.center.x, c.center.y, c.radius);
printf("Line: (%d, %d) - (%d, %d)\n", l.start.x, l.start.y, l.end.x, l.end.y);
return 0;
}
在这个例子中,我们定义了三个结构体Point、Circle和Line,它们分别表示一个点、一个圆和一条线段。
然后我们定义了一个指针函数move,它接受一个指向任意类型对象的指针obj和两个整型参数dx和dy,并根据obj的参数进行不同的动作。
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
typedef struct {
int radius;
Point center;
} Circle;
typedef struct {
Point start;
Point end;
} Line;
void move(void* obj, int dx, int dy, int type) {
if (obj == NULL) {
return;
}
if (type == 1) { // 圆
Circle* c = Circle* obj;
c->center.x += dx;
c->center.y += dy;
} else if (type == 2) { // 线
Line* l = (Line*)obj;
l->start.x += dx;
l->start.y += dy;
l->end.x += dx;
l->end.y += dy;
}
}
int main() {
// 创建一个圆和一条线
Circle c = { 5, { 0, 0 } };//
Line l = { { 0, 0 }, { 5, 5 }};
// 移动圆和线
move(&c, 2, 2, 1);
move(&l, 2, 2, 2);
// 输出移动后的圆和线
printf("Circle: (%d, %d), radius = %d\n", c.center.x, c.center.y, c.radius);
printf("Line: (%d, %d) - (%d, %d)\n", l.start.x, l.start.y, l.end.x, l.end.y);
return 0;
}
#include <stdio.h>
int ADD(int a, int b){
return a+b;
}
int SUB(int a, int b){
return a-b;
}
typedef struct{
int (*p_add)(int, int);
int (*p_sub)(int, int);
}OP;
int CALC(int a, int b, int (*calc_callback)(int , int )){
return calc_callback(a, b);
}
typedef int (*calc_callback)(int , int );
int CALC1(int a, int b, calc_callback calc){
return calc(a, b);
}
calc_callback calcPtr[4] = {&ADD, &SUB};
void initOp(OP *op){
op->p_add = ADD;
op->p_sub = SUB;
}
int main(){
OP op; // 声明并初始化OP类型的变量op
initOp(&op); // 将op作为参数传递给initOp函数
printf("Hello, World! %d\n", CALC(2,6,ADD));
printf("Hello, World! %d\n", CALC1(2,6,ADD));
printf("Hello, World! %d\n", calcPtr[0](2,6));
printf("Hello, World! %d\n", op.p_add(3,2)); // 使用op->p_add函数指针
return 0;
}