C语言结构体2

#include <stdio.h>
#define LEN 20

struct names{
    char first[LEN];
    char last[LEN];
};

struct guy{
    struct names handle;
    char favfood[LEN];
    char job[LEN];
    float income;
};

struct people{
    struct names handle;
    char favfood[LEN];
    char job[LEN];
    float income;
    int (*showinfo2)(struct people *him);
};

int show2(struct people *him){
    // 用点还是->,是由这个前面是什么东西决定。如果它是指针,就用->,否则就用.
    printf("%s, %s, %s, %s, %.2f \n", him->handle.first, him->handle.last, him->favfood, him->job, him->income);
    // printf("%s, %s, %2f",  him->favfood, him->job, him->income);
    // printf(" %s, %s, %f", him.favfood, him.job, him.income);
}

int show(struct guy *him){
    // 用点还是->,是由这个前面是什么东西决定。如果它是指针,就用->,否则就用.
    printf("%s, %s, %s, %s, %.2f \n", him->handle.first, him->handle.last, him->favfood, him->job, him->income);
    // printf("%s, %s, %2f",  him->favfood, him->job, him->income);
    // printf(" %s, %s, %f", him.favfood, him.job, him.income);
}

int main(void){
    struct guy fellow[2]={
        {
            {"Even" , "Villard"},
            "tomato",
            "testing",
            12000.22
        },
        {
            {"first" , "second"},
            "no"
            "coding",
            120.12
        },
    };

    struct guy *him;
    printf("address %p, %p \n", &fellow[0], &fellow[1]);
    him = &fellow[0];
    show(him);
    him++;
    show(him);

    struct people pp={
        .handle = {"p1", "p2"},
        .favfood = "nothing",
        .job = "dance",
        .income = 3333300.22
    };
    show(&pp);
    pp.showinfo2 = show2;
    pp.showinfo2(&pp);
    
    return 0;
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值