結構體對於後面繼續學習數據結構具有非常的重要意義,如果沒有掌握好結構體的內容後面學習數據結構的時候就會比較痛苦。如果本文介紹的不夠詳細想了解更多的孩童的可以去這裏xio習結構體。同時學習好結構體對於後面學習C語言的數據結構裏面的鏈表也有非常大的好處。全文共5541計字,閱讀時需要一定耐心,希望閱讀完本文後你能有所收穫。
一. 結構體的簡歷
結構體的聲明只是進行框架的描繪,不會在內存中佔用存儲空間,只有定義第一個真正的結構體變量的時候纔會佔用內容空間。
結構體既可以放在main函數裏面也可以放在main外面全局使用。
二、結構體的結構及定義方法
C語言的結構體由struct函數、結構體標籤、結構體成員、結構體變量這幾部分組成。在定義結構體的時候可以進行部分省略,但是注意結構體標籤、結構體成員、結構體變量這仨每次最少要出現兩個。如果省略標籤就必須要有成員和變量。同理,如果省略成員則必須要有標籤和變量。。有的也將結構體標籤叫做結構體名,結構體成員就是定義普通的整形、浮點型、字符型的變量。
介紹完結構體的框架結構之後我們再來看一下在C語言中結構體的定義及讀取結構體成員的方法。
#include <stdio.h>
struct Peoples
{
char name[50];
int age;
float hight;
}people;
int main()
{
printf("請輸入姓名\n");
scanf("%s",&people.name);
printf("請輸入年齡\n");
scanf("%d",&people.age);
printf("請輸入身高\n");
scanf("%f",&people.hight);
printf("=====輸入完畢=====\n");
printf("姓名:%s\n",people.name);
printf("年齡:%d\n",people.age);
printf("身高:%0.2f\n",people.hight);
return 0;
}
上面說過結構體的變量名可以省略,但是在必須通過其他方式說明,下面演示省略變量名的結構體的創建及成員讀取。
#include <stdio.h>
struct Peoples{
char name[50];
int age;
float hight;
};
struct Peoples people;
int main()
{
printf("請輸入姓名\n");
scanf("%s",&people.name);
printf("請輸入年齡\n");
scanf("%d",&people.age);
printf("請輸入身高\n");
scanf("%f",&people.hight);
printf("=====輸入完畢=====\n");
printf("姓名:%s\n",people.name);
printf("年齡:%d\n",people.age);
printf("身高:%0.2f\n",people.hight);
return 0;
}
【運行結果】因爲上面是使用的兩種不同的方法定義結構體,但是其作用相同,所以運行結果相同。
三、結構體的初始化
1.結構體的初始化
在C語言中我們在定義普通整形、字符型變量的時候可以先定義變量再賦值,也可以在定義的時候賦初始化值。下面通過這串來看一下在C語言中結構體的初始化方法。
#include <stdio.h>
struct Peoples{
char name[50];
int age;
float hight;
};
struct Peoples people;
//注意!!開始賦值了,賦值的時候要和結構體的聲明的變量的順序相同
struct Peoples people={
"張三",
18,
170.5
};
int main()
{
printf("=====輸入完畢=====\n");
printf("姓名:%s\n",people.name);
printf("年齡:%d\n",people.age);
printf("身高:%0.2f\n",people.hight);
return 0;
}
【運行結果】
2. 結構體指定成員值的初始化
對指定成員值進行初始化的時候可以不按照結構體成員聲明的順序進行初始化。因爲我們在對指定的成員進行初始化的時候已經指出其變量名,所以可以不按照聲明順序進行初始化。
#include <stdio.h>
struct Peoples{
char name[50];
int age;
float hight;
};
struct Peoples people;
//.號運算符和成員名
struct Peoples people={
.hight=170.5,
.age=18,
.name="張三"
};
int main()
{
printf("姓名:%s\n",people.name);
printf("年齡:%d\n",people.age);
printf("身高:%0.2f\n",people.hight);
return 0;
}
【運行結果】
四、結構體套娃——結構體嵌套
數組裏面嵌套數組就有了二維數組,結構體裏面嵌套結構體就可以對信息進行更詳細的表達。
#include <stdio.h>
struct Date{
int years;
int month;
int day;
}date;
struct Peoples{
char name[50];
struct Date date;
}people;
struct Peoples people = {"法外狂徒張三",{2020,5,12}};
int main(){
printf("name:%s\n",people.name);
printf("birthday:%d-%d-%d",people.date.years,people.date.month,people.date.day);
return 0;
}
【運行結果】
五、結構體數組
學習數組的時候我們知道數組的每一個元素都是一個基礎類型,在結構體數組中數組的每一個元素將是一個結構體。結構體數組的用處是什麼呢?一個結構體我們可以表示出一個人的信息。那麼如果一個結構體數組就可以表示出一羣人的信息。
定義結構體數組有兩種方式,第一種是在定義結構體的時候進行定義。第二種是先定義一個結構體類型,再以此類型定義一個結構體數組。兩種定義方式的結構如下:
#include <stdio.h>
struct birthday{
int year;
int month;
int day;
};
struct student{
char name[10];
int age;
long stu_number;
struct birthday bir;
};
int main()
{
int a,i;
printf("How many students do you want to input?:");
scanf("%d",&a);
struct student stu[a];
printf("Please input your information\n");
for(i=0;i<a;i++)
{
printf("student%d's name:",i+1);
scanf("%s",&stu[i].name);
printf("student%d's age :",i+1);
scanf("%d",&stu[i].age);
printf("student%d's student number:",i+1);
scanf("%ld",&stu[i].stu_number);
printf("student%d's birthday's year:",i+1);
scanf("%d",&stu[i].bir.year);
printf("student%d's birthday's month:",i+1);
scanf("%d",&stu[i].bir.month);
printf("student%d's birthday's day:",i+1);
scanf("%d",&stu[i].bir.day);
}
printf("\n====Yourself information====\n\n");
for(i=0;i<a;i++)
{
printf("student%d's name:%s\n",i+1,stu[i].name);
printf("student%d's age :%d\n",i+1,stu[i].age);
printf("student%d's student number:%ld\n",i+1,stu[i].stu_number);
printf("student%d's birthday:%d-%d-%d",i+1,stu[i].bir.year,stu[i].bir.month,stu[i].bir.day);
}
return 0;
}
【運行結果】
六、結構體指針
指天指地指空氣,在C語言中指針無所不能,無所不指。在結構體中用好指針在鏈表中可以少喫很多的苦頭。因爲鏈表中的節點就是一個結構體,當然這都是後話了。在C語言中結構體有以下兩種表達方式
#include<stdio.h>
struct people{
char a;
int b;
};//結構體聲明
struct people Peo = {
'A',
123
};
int main()
{
struct people *p;//定義結構體指針
p=&Peo;//將結構體地址賦值給指針
printf("%c\n",(*p).a);//第一種讀取方式
printf("%d",p->b);//第二種讀取方式
return 0;
}
【運行結果】
結構體指針練習程序
#include <stdio.h>
struct Date{
int years;
int month;
int day;
}date;
struct Peoples{
char name[50];
struct Date date;
}people;
struct Peoples people = {"法外狂徒張三",{2020,5,12}};
int main(){
struct Peoples *p;
p=&people;
printf("name:%s\n",people.name);
printf("birthday:%d-%d-%d",p->date.years,(*p).date.month,people.date.day);
return 0;
}
【運行結果】
如果你有其他疑問可以在下方留言,也可以私信我哦。