結構體的簡單知識點

2021-08-04 09:52:35 字數 1414 閱讀 4800

包括結構體的基本知識、結構體的記憶體對齊原理、結構體巢狀和結構體指標

#include 

#include

// struct 是乙個c語言的乙個關鍵字,用來宣告乙個結構體型別

// 宣告了乙個名字叫 student 的結構體型別

// 有兩個成員,分別是整型的 id(學號) 字元陣列 name(名字)

// struct 只是宣告了這樣一種資料型別,和普通的資料型別比如int、char

// 一樣本身是沒有分配空間的,只有在定義變數的時候才會為變數分配空間

struct student

; //用struct 宣告型別的時候要有 ;號,和函式相區分

#if 0

// 變數定義2:直接在結構型別宣告的時候定義變數

struct teacher

t1,t2; // 定義了兩個老師的變數 變數名叫 t1、t2

// 變數定義3:結構型別可以不要名字,也就是說不能在其他地方使用了

// 只能在型別宣告的時候定義變數

struct

a, b; // 定義結構體變數a和b

// int main()

#endif

// 結構體變數的初始化和成員使用

int main1()

; struct student stu2 = ;

// 結構的成員變數的使用,結構變數使用成員變數,需要點 . 指明使用的是哪乙個成員

printf ("id = %d, name = %s\n", stu.id, stu.name);

}int main()

#include 

struct a

;struct b

;int main()

#include 

struct a

;struct b

;int main()

#include 

#include

struct student

;int main1()

void printa(struct student a)

void printb(struct student *a)

int main()

; struct student b;

b = a;// 結構體變數之間可以直接賦值

// 結構體變數之間可以直接賦值導致結構變數可以直接作為函式引數進行傳遞

printb(&b);

printf ("id = %d, name = %s\n", b.id, b.name);

return

0;}

結構體知識點總結

對於結構體的知識點,我主要分為以下幾點來講解 結構體的宣告 結構體型別的建立 結構體成員的訪問 結構體的初始化 結構體內存對齊 位段。1.結構體的宣告 struct stu 分號不能丟2.結構體型別的建立 有兩種建立的方法,第一種 struct stu struct stu s1 定義了乙個結構體變...

結構體知識點梳理

typedef struct teacher teacher int main strcpy t0.name,t1name t1.操作符 定址操作,計算name相對於大變數的偏移量,在cup中計算,沒有操作記憶體 通過指標的方式 操作 記憶體空間 teacher p null p t0 printf...

知識點總結 7 結構體

if a 0 推薦 a 1 逼近條件 變數意義單一函式意義單一 資料區存放 全域性變數 靜態區域性 全域性變數 字串常量 區 堆區 棧區編譯時進行型別檢查 陣列名 例外 1 siezof 整個大小 首元素位址 函式在棧幀中退出時 棧幀被 但其中的資料還有殘留 只有在另乙個函式需開闢此棧幀前 wind...