C 结构体 结构体指针 指针函数 学习记录

#include<stdio.h>
struct data{
    int x;
    int y;
};
struct data1{
    int x;
    int y;
}p1,p2;
typedef struct{
    int x;
    int y;
}data3;

//函数指针,指向函数
int(*func)(int a, int b);
int bar(int a, int b)
{
    return a + b;
}
int foo(int a, int b)
{
    return a;
}

//结构体中含有指针函数
struct hello_driver {
int(*test)(struct data *);
};
int hello(struct data *h){
    return h->x;
}

//含有指针函数的结构体直接初始化
struct hello_driver driver2={
    .test=hello,
};
void main(){
    struct data hello1;
    hello1.x=10;
    printf("hello world %d\n",hello1.x);
    p1.x = 20;
    printf("hello world %d\n",p1.x);
    data3 p2;
    p2.x =30;
    printf("hello world %d\n",p2.x);
    //函数指针指向bar函数
    func=bar;
   //调用函数指针
    printf("hello world %d\n",func(1,2));
    func=foo;
    printf("hello world %d\n",func(5,6)); 
   //含有函数指针的结构体
    struct hello_driver driver;
    driver.test=hello;
    struct data *h;
    h=&hello1;
    printf("hello world %d\n",driver.test(h));
    printf("hello world %d\n",driver2.test(&hello1));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值