C语言结构体2

发布时间:2024年01月13日
#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;
}

文章来源:https://blog.csdn.net/xinyuebaihe/article/details/135577594
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。