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);
阅读全文
1 0
- C语言结构体
- C语言:结构体
- C语言:结构体
- C语言--结构体
- c语言结构体
- c语言 结构体
- C语言结构体
- C语言结构体
- C语言--结构体
- c语言 结构体
- c语言----结构体
- C语言--结构体
- c 语言 结构体
- c语言结构体
- C语言-结构体
- c语言结构体
- C语言结构体
- c 语言 结构体
- 《Python基础教程》学习笔记——字符串
- 【NOIP2017模拟8.8A组】Trip(trip)
- Android 7.0解决抓取不到https请求的问题
- Django学习笔记10-URL的名称空间
- 注意多个实体类放进list的问题
- C语言结构体
- POI给合并的单元格设置边框
- 从共享征信黑名单开始:LinkEye想打造基于区块链的征信联盟
- MyEclipse使用总结——MyEclipse10安装SVN插件(转)
- HideInInspector SerializeField
- java上转型对象分析
- Android开发中在一个Activity中关闭另一个Activity
- 在Spark Streaming上使用Spark Mllib的思路实现
- Java Object类详解