【學習筆記】【C語言】結構體,學習筆記c語言
1.定義結構體變數的3種方式
1> 先定義類型,再定義變數(分開定義)
struct Student
{
int age;
};
struct Student stu;
2> 定義類型的同時定義變數
struct Student
{
int age;
} stu;
struct Student stu2;
3> 定義類型的同時定義變數(省略了類型名稱)
struct
{
int age;
} stu;
2.結構體類型的範圍
1> 定義在函數外面:全域有效(從定義類型的那行開始,一直到檔案結尾)
2> 定義在函數(代碼塊)內部:局部有效(從定義類型的那行開始,一直到代碼塊結束)
3.代碼
1>結構體
1 /* 2 數組:只能由多個相同類型的資料構成 3 4 結構體:可以由多個不同類型的資料構成 5 */ 6 #include <stdio.h> 7 8 int main() 9 {10 //int ages[3] = {[2] = 10, 11, 27};11 12 13 //int ages[3] = {10, 11, 29};14 15 // 1.定義結構體類型16 struct Person17 { // 裡面的3個變數,可以稱為是結構體的成員或者屬性18 int age; // 年齡19 double height; // 身高20 char *name; // 姓名21 };22 23 // 2.根據結構體類型,定義結構體變數24 struct Person p = {20, 1.55, "jack"};25 p.age = 30;26 p.name = "rose";27 28 printf("age=%d, name=%s, height=%f\n", p.age, p.name, p.height);29 30 /* 錯誤寫法31 struct Person p2;32 p2 = {30, 1.67, "jake"};33 */34 35 struct Person p2 = {.height = 1.78, .name="jim", .age=30};36 //p2.age = 25;37 38 return 0;39 }
2>結構體記憶體分析
1 #include <stdio.h> 2 int main() 3 { 4 5 6 return 0; 7 } 8 9 // 補齊演算法10 void test1()11 {12 struct Student13 {14 int age;// 4個位元組15 16 char a;17 18 //char *name; // 8個位元組19 };20 21 struct Student stu;22 //stu.age = 20;23 //stu.name = "jack";24 // 補齊演算法(對齊演算法)25 // 結構體所佔用的儲存空間 必須是 最大成員位元組數的倍數26 27 int s = sizeof(stu);28 printf("%d\n", s);29 }30 31 // 結構體記憶體細節32 void test()33 {34 // 1.定義結構體類型(並不會分配儲存空間)35 struct Date36 {37 int year;38 int month;39 int day;40 };41 42 // 2.定義結構體變數(真正分配儲存空間)43 struct Date d1 = {2011, 4, 10};44 45 46 struct Date d2 = {2012, 8, 9};47 48 // 會將d1所有成員的值對應地賦值給d2的所有成員49 d2 = d1;50 d2.year = 2010;51 52 printf("%d - %d - %d\n", d1.year, d1.month, d1.day);53 54 printf("%d - %d - %d\n", d2.year, d2.month, d2.day);55 /*56 printf("%p - %p - %p\n", &d1.year, &d1.month, &d1.day);57 58 int s = sizeof(d1);59 printf("%d\n", s);60 61 */62 }
3>注意點
1 #include <stdio.h> 2 // 從這行開始,一直到檔案結尾,都是有效(跟全域變數一樣) 3 struct Date 4 { 5 int year; 6 int month; 7 int day; 8 }; 9 10 int a; 11 12 void test2() 13 { 14 struct Date 15 { 16 int year; 17 }; 18 // 這裡使用的是test2函數內部的struct Date類型 19 struct Date d1 = {2011}; 20 21 22 // 結構體類型也是有範圍,從定義類型的那一行開始,一直到代碼塊結束 23 struct Person 24 { 25 int age; 26 }; 27 28 struct Person p; 29 30 a = 10; 31 } 32 33 int main() 34 { 35 struct Date d1 = {2009, 8, 9}; 36 37 38 test2(); 39 40 // 不能使用test2函數中定義的類型 41 // struct Person p2; 42 43 return 0; 44 } 45 46 // 定義結構體變數 47 void test() 48 { 49 // 定義結構體變數的第3種方式 50 struct { 51 int age; 52 char *name; 53 } stu; 54 55 struct { 56 int age; 57 char *name; 58 } stu2; 59 60 61 /*結構體類型不能重複定義 62 struct Student 63 { 64 int age; 65 }; 66 67 struct Student 68 { 69 double height; 70 }; 71 72 struct Student stu; 73 */ 74 75 /* 錯誤寫法:結構體類型重複定義 76 struct Student 77 { 78 int age; 79 double height; 80 char *name; 81 } stu; 82 83 struct Student 84 { 85 int age; 86 double height; 87 char *name; 88 } stu2;c 89 */ 90 91 /* 92 這句代碼做了兩件事情 93 1.定義結構體類型 94 2.利用新定義好的類型來定義結構體變數 95 */ 96 // 定義變數的第2種方式:定義類型的同時定義變數 97 /* 98 struct Student 99 {100 int age;101 double height;102 char *name;103 } stu;104 105 struct Student stu2;106 */107 108 /*109 // 定義變數的第1種方式:110 // 1.類型111 struct Student112 {113 int age;114 double height;115 char *name;116 };117 118 // 2.變數119 struct Student stu = {20, 1.78, "jack"};120 */121 }