一个做网页的网站,帮朋友做网站不给钱,酒店网站模版,中国建筑集团招聘信息引入 使用传统技术解决 需要定义多个变量或数组
结构体与结构体变量的关系示意图
类似Java类中的对象(结构体)与属性(结构体变量) 一切物体都可以看作对象(结构体) 补充:C语言数据类型
简单使用案例 代码 Cat是我们自己定义的数据类型 struct Cat cat1;//创建struct Cat的…引入 使用传统技术解决 需要定义多个变量或数组
结构体与结构体变量的关系示意图
类似Java类中的对象(结构体)与属性(结构体变量) 一切物体都可以看作对象(结构体) 补充:C语言数据类型
简单使用案例 代码 Cat是我们自己定义的数据类型 struct Cat cat1;//创建struct Cat的一个变量
练习
输入字符串,匹配person信息
#includestdio.h
#includestring.h
//结构体 struct
//输入字符串,匹配person信息
#define SIZE 100
int main()
{char s[SIZE];printf(please input s:\n);gets(s);struct Person{char *name;int age;char *car;};//不要忘记;struct Person p1;struct Person p2;p1.namedq;p1.age18;p1.carWT;p2.nameww;p2.age12;p2.carET;if(strcmp(s,p1.name)0){printf(name%s age%d car%s\n,p1.name,p1.age,p1.car);}else if(strcmp(s,p2.name)0){printf(name%s age%d car%s\n,p2.name,p2.age,p2.car);}else{printf(no found\n);}getchar();//entergetchar();return 0;
}结构体和结构体变量的区别和联系 结构体变量在内存中的布局 结构体成员
声明结构体/结构体包含的变量 结构体成员的基本介绍 注意事项
结构体定义的三种形式
创建结构体的三种方式 方式1: 方式2: 方式3:匿名结构体
结构体成员的获取和赋值
方式1: 方式2: 使用案例:
整体使用代码
#includestdio.h
//定义结构体的方式结构体成员的定义和赋值方式int main()
{//定义结构体的方式1struct A{int aage;char*aname;};struct A a{99,A};printf(age%d name%s\n,a.aage,a.aname);//定义结构体的方式2struct B{int bage;char*bname;}b;//没有b{90,B}; 因为struct B是一个整体,只有struct B b{90,B}b.bage10;b.bnameB;printf(age%d name%s\n,b.bage,b.bname);//定义结构体的方式3struct{int cage;char*cname;}c{88,C};printf(age%d name%s\n,c.cage,c.cname);getchar();return 0;
}结构体案例练习1 补充:spintf()函数
Sprint(字符串,”%原先的类型”,要转换的数据类型变量) 将其他数据类型转换到字符串中 “%8.2f”代表一共有8位,2代表小数的位置,不够用空格补齐
结构体是值传递,在传递时会拷贝一份值,对原来的值没有影响
#includestdio.h
//小狗案例
char*say(struct Dog dog);
struct Dog{//结构体char *name;int age;double weight;
}dog{dog,3,10.8};
int main()
{char*ssay(dog);printf(结果:%s\n,s);getchar();return 0;
}
char*say(struct Dog dog)//形参为struct Dog类型{static char info[100];//局部变量/*Sprint(字符串,”%原先的类型”,要转换的数据类型变量)将其他数据类型转换到字符串中*/sprintf(info,name%s age%d weight%.2f,dog.name,dog.age,dog.weight);dog.nameMMMM;//对输出的内容没有影响//结构体是值传递,在传递时会拷贝一份值,对原来的值没有影响return info;
}结构体案例练习2 代码
#includestdio.h
//结构体--盒子案例
char* info(struct Box box);
struct Box{double l;//长double w;//宽double h;//高
};
int main()
{double l;//长double w;//宽double h;//高printf(please input the information of box:\n);//double %lfscanf(%lf %lf %lf,l,w,h);struct Box box{l,w,h};printf(information:%s\n,info(box));getchar();//entergetchar();return 0;}
char* info(struct Box box)
{static char s[100];//局部变量--staticdouble vbox.l*box.w*box.h;sprintf(s,l%.2f w%.2f h%.2f v%.2f,box.l,box.w,box.h,v);return s;
}结构体案例练习3 代码: strcmp()0与!strcmp()效果一样 !strcmp()//0代表假,非0为真,所以使用!取反(真取反为0)
visitor.name是数组,本身就是地址,而visitor.age是整型变量,要使用visitor.age取地址(优先级:.高于)
*visitor:获取的是visitor本身
#includestdio.h
#includestring.h
//结构体--景区门票案例:使用地址传递//函数原型/声明
double ticket(struct Visitor*visitor);
struct Visitor{char name[10];int age;double pay;//需要支付的门票
} visitor;
int main()
{int i1;while(1){printf(please input information%d:name age\n,i);scanf(%s %d,visitor.name,visitor.age);if(!(strcmp(visitor.name,n))){break;//结束while循环}visitor.payticket(visitor);printf(pay%.2f\n,visitor.pay);i;}printf(exit\n);getchar();//entergetchar();return 0;
}
//使用结构体指针,传递地址,提高效率
double ticket(struct Visitor*visitor)
{//使用*visitor取的具体的visitor变量if((*visitor).age18)return 20;elsereturn 0;
}