第十章 結構體與共用體
第二節 結構體數組,結構體指針
1. 結構體數組
- 單獨寫成一行來定義
struct student stu[3];
- 定義結構的時候順便定義結構體數組
struct student {
int num; //學號
char name[100]; //姓名
int sex; //性別,女:0, 男:1
int age; //年齡
char address[100]; //地址
struct date birthday; //生日,結構體類型變量
}stu[3];
- 定義結構體數組的時候可以同時進行初始化
#include <iostream>
#include <cstring>
struct date {
int year;
int month;
int day;
};
struct student {
int num; //學號
char name[100]; //姓名
int sex; //性別,女:0, 男:1
int age; //年齡
char address[100]; //地址
struct date birthday; //生日,結構體類型變量
};
int main(int argc, char *argv[]) {
struct student stu[3]{
{100, "Felaim", 0, 18, "aa", {2002, 11, 15}},
{101, "Zhang", 1, 19, "bb", {2001, 7,8}},
{99, "Wang", 1, 17, "cc", {2003, 12, 12}}
};
stu[1].age++;//20
printf("name: %s\n age: %d\n", stu[1].name, stu[1].age);
return 0;
}
2. 結構體指針
-
就是結構體變量的指針,這個指針肯定是指向該結構體變量所佔據的內存的起始地址,
結構體變量指針也可以指向結構體數組的元素,因爲結構體數組中的每個元素就相當於一個結構體變量 -
如何通過結構體變量的指針來訪問結構體變量的成員呢?
-
(*pstu).age, 第一種使用(*pstu),因爲*優先級不如結構體成員運算符"."高,所以這裏要添加括號
-
pstu->age, 第二種使用pstu->, 其中這個"->"叫指向結構體成員運算符,優先級非常高
#include <iostream>
#include <cstring>
struct date {
int year;
int month;
int day;
};
struct student {
int num; //學號
char name[100]; //姓名
int sex; //性別,女:0, 男:1
int age; //年齡
char address[100]; //地址
struct date birthday; //生日,結構體類型變量
};
int main(int argc, char *argv[]) {
struct student stu;//定義一個結構體變量
struct student *pstu;//定義一個結構體變量的指針
//現在,pstu就指向了stu,stu裏邊的內容改變了,就等於pstu所指向的內容改變了
pstu = &stu;
strcpy(stu.name, "Felaim");
stu.age = 18;
// *pstu ==> stu
(*pstu).num = 100;
printf("name: %s\n", pstu->name);
printf("age: %d\n", stu.age);
return 0;
}
- 指向結構體數組的指針
#include <iostream>
#include <cstring>
struct date {
int year;
int month;
int day;
};
struct student {
int num; //學號
char name[100]; //姓名
int sex; //性別,女:0, 男:1
int age; //年齡
char address[100]; //地址
struct date birthday; //生日,結構體類型變量
};
int main(int argc, char *argv[]) {
struct student stu[3]{
{100, "Felaim", 0, 18, "aa", {2002, 11, 15}},
{101, "Zhang", 1, 19, "bb", {2001, 7, 8}},
{99, "Wang", 1, 17, "cc", {2003, 12, 12}}
};
struct student *pstu;
pstu = stu;//數組名代表的是數組首地址,數組中數據在內存中都是挨着的
int i;
for (i = 0; i < 3; i++) {
printf("name: %s\n", pstu->name);
printf("num: %d\n", pstu->num);
pstu++;
}
// 一個結構體有224個字節
printf("sizeof struct: %d bytes\n", sizeof(struct student));
return 0;
}
- 說明
-
printf("%d\n", (++pstu)->num);//++先加後用, 101
-
printf("%d\n", (pstu++)->num);//++先用後加, 100
-
pstu = &stu[1];//不能讓pstu指向數組元素中的成員(pstu = &stu[1].name)
3. 用指向結構體的指針做函數參數
#include <iostream>
#include <cstring>
struct date {
int year;
int month;
int day;
};
struct student {
int num; //學號
char name[100]; //姓名
int sex; //性別,女:0, 男:1
int age; //年齡
char address[100]; //地址
struct date birthday; //生日,結構體類型變量
};
void func1(struct student *pd){
pd->age =120;
}
int main(int argc, char *argv[]) {
struct student stu[3]{
{100, "Felaim", 0, 18, "aa", {2002, 11, 15}},
{101, "Zhang", 1, 19, "bb", {2001, 7, 8}},
{99, "Wang", 1, 17, "cc", {2003, 12, 12}}
};
struct student *pstu;
pstu = stu;
func1(pstu);
printf("age: %d\n", (pstu)->age);
printf("age: %d\n", stu[0].age);
return 0;
}
- 實例
#include <iostream>
#include <cstring>
struct date {
int year;
int month;
int day;
};
struct student {
int num; //學號
char name[100]; //姓名
int sex; //性別,女:0, 男:1
int age; //年齡
char address[100]; //地址
struct date birthday; //生日,結構體類型變量
};
void func1(struct student *pd) {
pd->age = 120;
}
void func2(struct student pd) {
strcpy(pd.address, "123");
}
int main(int argc, char *argv[]) {
struct student stu[3]{
{100, "Felaim", 0, 18, "aa", {2002, 11, 15}},
{101, "Zhang", 1, 19, "bb", {2001, 7, 8}},
{99, "Wang", 1, 17, "cc", {2003, 12, 12}}
};
struct student *pstu;
pstu = stu;
func1(pstu);
printf("age: %d\n", (pstu)->age);
printf("age: %d\n", stu[0].age);
// 發現並沒有改變,因爲是值傳遞,改變的形參的內存,並沒有改變實參的內存,這是兩塊內存
// 將完整的結構體變量直接傳給func中,相當於做了內存內容的拷貝,開銷很大,費時費空間
func2(stu[0]);
printf("address: %s\n", stu[0].address);
return 0;
}
- 建議函數參數儘量使用地址和指針