C语言结构体

来源:互联网 发布:linux网速测试命令 编辑:程序博客网 时间:2024/06/03 16:39
#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 = 12size 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);