結構體筆記:
1.結構體:其實也是一種資料型別,但是是一種複雜的資料型別,把一些基本型別資料組合在一起形成的乙個新的復合資料型別
2.定義結構體的形式:
struct student1
;
第二種:前面定義了結構體的名字,最後面又定義了結構體變數
struct student2
;st2
第三種:沒有結構體的名字
struct
;
3.如何使用結構體變數:
賦值和初始化
1定義的同時可以整體賦值
2如果定義完之後,只能單個的賦初值
#include
"pch.h"
#include
struct student1
;int
main()
;//定義的同時可以整體賦值
struct student1 st2;
//單獨定義,自己的小錯誤,老是忘記寫結構體的名字
st2.age =10;
st2.score =88;
st2.*** =
'f';
}
4.如何取出結構體變數中的每乙個成員
1.結構體變數名.成員名
2.指標變數的方式: 指標名->成員名
#include
"pch.h"
#include
//這只是定義了乙個新的資料型別,並沒有定義變數
struct student1
;int
main()
;//定義的同時可以整體賦值
//struct student1 st2; //單獨定義,自己的小錯誤,老是忘記寫結構體的名字
= 10; //
= 88;
= 'f';
// 定義方式
st.age =80;
// 第一種方式
struct student1* pst =
&st;
// 第二種指標定義法(更常用)
pst->age =80;
//pst->age在計算機內部會被轉化成(*pst).age,沒有為什麼,這就是->的含義,這是一種硬性規定。
//所以pst->age等價於(*pst).age,也等價於st.age
//pst->age的含義: pst所指向的那個結構體變數中的age成員。(重要)
st.age =
66.6f
;在c語言中預設是double型別,如果希望是float型別,則需要在數字後面加f或者f
return0;
}
5.指標變數和結構體
#include
"pch.h"
#include
//定義函式之前要先宣告,剛才沒宣告,一直顯示找不到識別符號,注意!!!!!
void
inputstudent
(struct student1 * pstu)
;void
outputstudent
(struct student1 ss)
;struct student1
;///最後的這個分號不能省略
intmain()
void
inputstudent
(struct student1 * pstu)
void
outputstudent
(struct student1 ss)
6.結構體巢狀結構體
//結構體中的成員可以是另乙個結構體
//例如:每個老師輔導乙個學員,乙個老師的結構體中,記錄乙個學生的結構體
#include
"pch.h"
#include
#include
using
namespace std;
struct student
;struct teacher
;int
main()
結構體學習筆記
什麼是結構體 1 不同型別的變數集合 2 結構體的目的是為了描述乙個物件,為了把乙個物件描述清楚,必須要說出這個物件的很多屬性,為了表示這些屬性,就要用到不同的變數,且變數不止乙個。工人 工號 姓名 性別 年齡 基本工資 如何來定義結構體 struct 結構體名 struct worker 定義乙個...
結構體學習筆記
以下記載了在初學結構體時犯下的一些錯誤。先來一些雜識 structf struct students void main basic knowledge 錯誤一 結構體內用本身定義實體 1 struct student 2 因為在結構體執行完之前,是沒有記憶體位址空間的。而第五行中定義了乙個實體,顯...
結構體和共用體學習筆記
為什麼需要結構體 傳統解決辦法 陣列是相同型別資料的集合。結構體可以存放不同型別的資料。結構體和結構體變數的區別和聯絡 1 結構體是自定義的資料型別,表示的是一種資料型別,2 結構體變數是乙個具體變數 int num1l int為資料型別,而num1是具體的int變數3 結構體變數在同一記憶體區域 ...