C语言结构体

#include <stdio.h>
#include <string.h>
struct Book{
    char title[128];
    char author[40];
    float price;
    unsigned int date;
    char publisher[40];
}book;



int main(){


    strcpy(book.title, "三生三世十里桃花");
    strcpy(book.author, "唐七");
//  book.anthor = "唐七" ; //错误 
    book.price = 35.00;
    book.date = 20170801;
    strcpy(book.publisher,"湖南文艺出版社");


    printf("书名:%s\n", book.title);
    printf("作者:%s\n", book.author);
    printf("价格:%.2f\n", book.price);
    printf("日期:%d\n", book.date);
    printf("出版社:%s\n", book.publisher);




    return 0;
}
***输出:***

书名:三生三世十里桃花
作者:唐七
价格:35.00
日期:20170801
出版社:湖南文艺出版社


结构对齐

#include <stdio.h>

int main(){

    struct A{
        char a;
        int b;
        char c;
    }a = {
        'x',
        520,
        '0'
    };
    struct B{
        char a;
        char c;
        int b;

    }b = {
        'x',
        '0',
        520,

    };
    printf("size of a = %d\n", sizeof(a));
    printf("size of b = %d\n", sizeof(b));


    return 0;
}
**输出:**

size of a = 12
size of b = 8

这里写图片描述


#include <stdio.h>
#include <string.h>

struct Date{
    int year;
    int month;
    int day;
};
struct Book{
    char title[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
}book = {
    "三生三世十里桃花",
    "唐七",
    35.00,
    {2017,8,1},
    "湖南文艺出版社"
};



int main(){





    printf("书名:%s\n", book.title);
    printf("作者:%s\n", book.author);
    printf("价格:%.2f\n", book.price);
    printf("日期:%d-%d-%d\n", book.date.year, book.date.month, book.date.day);
    printf("出版社:%s\n", book.publisher);



    return 0;
}
**输出:**

书名:三生三世十里桃花
作者:唐七
价格:35.00
日期:2017-8-1
出版社:湖南文艺出版社

结构体指针

访问方法:

(*结构体指针).成员名
结构体指针 -> 成员名

demo:

#include <stdio.h>
#include <string.h>

struct Date{
    int year;
    int month;
    int day;
};
struct Book{
    char title[128];
    char author[40];
    float price;
    struct Date date;
    char publisher[40];
}book = {
    "三生三世十里桃花",
    "唐七",
    35.00,
    {2017,8,1},
    "湖南文艺出版社"
};



int main(){

    struct Book *pt;
    pt = &book;


    //*结构体指针).成员名
    printf("书名:%s\n", (*pt).title);
    printf("作者:%s\n", (*pt).author);
    printf("价格:%.2f\n", (*pt).price);
    printf("日期:%d-%d-%d\n", (*pt).date.year, (*pt).date.month, (*pt).date.day);
    printf("出版社:%s\n", (*pt).publisher);

    //结构体指针 -> 成员名

    printf("书名:%s\n", pt -> title);
    printf("作者:%s\n", pt -> author);
    printf("价格:%.2f\n", pt -> price);
    printf("日期:%d-%d-%d\n", pt -> date.year, pt -> date.month, pt -> date.day);
    printf("出版社:%s\n", pt -> publisher);


    return 0;
}
**输出:**

书名:三生三世十里桃花
作者:唐七
价格:35.00
日期:2017-8-1
出版社:湖南文艺出版社
书名:三生三世十里桃花
作者:唐七
价格:35.00
日期:2017-8-1
出版社:湖南文艺出版社

结构体的赋值

1.两个结构体之间 可以用 “=”赋值.

st1 = st2;

2.以结构体为返回类型和参数是结构体的类型的函数 赋值

struct strnamexxx getData (struct strnamexxx xxx){
    .......
    return xxx;
}

//-------------
 struct strnamexxx st1;
 st1 = getData(st1);

传递指向结构体变量的指针

void In(struct strnamexxx *xxx){
    xxx -> groupName = value;
    .......

}
void Out(struct strnamexxx *xxx){
    printf(xxx -> groupName)
    .......

}
//------------
 struct strnamexxx st1;
 In(st1);
 Out(st1);

 //动态分配内存
 struct strnamexxx *st2;
 st2 = (struct strnamexxx *) malloc(sizeof(struct Book));
 if(st2 == NULL) exit(0);
 In(st2);
 Out(st2);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值