引入
使用傳統技術解決
需要定義多個變量或數組
結構體與結構體變量的關系示意圖
類似Java類中的對象(結構體)與屬性(結構體變量)
一切物體都可以看作對象(結構體)
補充:C語言數據類型
簡單使用案例
代碼
Cat是我們自己定義的數據類型
struct Cat cat1;//創建struct Cat的一個變量
練習
輸入字符串,匹配person信息
#include<stdio.h>
#include<string.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.name="dq";p1.age=18;p1.car="WT";p2.name="ww";p2.age=12;p2.car="ET";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:
使用案例:
整體使用代碼
#include<stdio.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.bage=10;b.bname="B";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代表小數的位置,不夠用空格補齊
結構體是值傳遞,在傳遞時會拷貝一份值,對原來的值沒有影響
#include<stdio.h>
//小狗案例
char*say(struct Dog dog);
struct Dog{//結構體char *name;int age;double weight;
}dog={"dog",3,10.8};
int main()
{char*s=say(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.name="MMMM";//對輸出的內容沒有影響//結構體是值傳遞,在傳遞時會拷貝一份值,對原來的值沒有影響return info;
}
結構體案例練習2
代碼
#include<stdio.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 v=box.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本身
#include<stdio.h>
#include<string.h>
//結構體--景區門票案例:使用地址傳遞//函數原型/聲明
double ticket(struct Visitor*visitor);
struct Visitor{char name[10];int age;double pay;//需要支付的門票
} visitor;
int main()
{int i=1;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.pay=ticket(&visitor);printf("pay=%.2f\n",visitor.pay);i++;}printf("exit\n");getchar();//entergetchar();return 0;
}
//使用結構體指針,傳遞地址,提高效率
double ticket(struct Visitor*visitor)
{//使用*visitor取的具體的visitor變量if((*visitor).age>=18)return 20;elsereturn 0;
}