C語言全面梳理結(jié)構(gòu)體知識點
一、什么是結(jié)構(gòu)體
為了更好地模擬現(xiàn)實,需要把各種基本數(shù)據(jù)類型組合在一起構(gòu)成一種新的復(fù)合數(shù)據(jù)類型,我們把這種自定義的數(shù)據(jù)類型稱為結(jié)構(gòu)體。結(jié)構(gòu)體是程序員根據(jù)實際需求,把各種基本數(shù)據(jù)類型組合在一起構(gòu)成的一種新的復(fù)合數(shù)據(jù)類型。
二、結(jié)構(gòu)體的定義
結(jié)構(gòu)體的定義方式共有3種,這里只用最常見也最常用的一種方式定義,即:
struct 結(jié)構(gòu)體名
{
成員列表;
}; // 注意這里的分號不能省略
舉個例子:
struct Student { int age; float score; char gender; };
三、結(jié)構(gòu)體變量的定義
結(jié)構(gòu)體與我們經(jīng)常使用的基本數(shù)據(jù)類型一樣,它也是一種數(shù)據(jù)類型,所以我們可以用這一數(shù)據(jù)類型定義其對應(yīng)的變量,我們把用結(jié)構(gòu)體類型定義的變量稱為結(jié)構(gòu)體變量。定義結(jié)構(gòu)體變量的方式如下所示:
#include <stdio.h> struct Student // 定義結(jié)構(gòu)體 { int age; float score; char gender; }; int main() { struct Student stu; // 定義結(jié)構(gòu)體變量 return 0; }
四、結(jié)構(gòu)體變量的初始化
#include <stdio.h> struct Student // 定義結(jié)構(gòu)體 { int age; float score; char gender; }; int main() { struct Student stu = { 15, 66.5, 'M' }; // 初始化結(jié)構(gòu)體變量 return 0; }
五、結(jié)構(gòu)體變量的賦值
定義結(jié)構(gòu)體變量后,我們可以對其中的成員進行賦值操作,但與初始化不同的是,需要通過結(jié)構(gòu)體變量名.成員名
的方式對結(jié)構(gòu)體變量中的每個成員單獨賦值。代碼如下所示
#include <stdio.h> struct Student // 定義結(jié)構(gòu)體 { int age; float score; char gender; }; int main() { struct Student stu; // 定義結(jié)構(gòu)體變量 stu.age = 15; // 對結(jié)構(gòu)體變量中的age成員賦值 stu.score = 66.5; // 對結(jié)構(gòu)體變量中的score成員賦值 stu.gender = 'M'; // 對結(jié)構(gòu)體變量中的gender成員賦值 return 0; }
六、引用結(jié)構(gòu)體變量中的成員
方法1:結(jié)構(gòu)體變量名.成員名
#include <stdio.h> struct Student // 定義結(jié)構(gòu)體 { int age; float score; char gender; }; int main() { struct Student stu; // 定義結(jié)構(gòu)體變量 // 對各成員賦值 stu.age = 15; stu.score = 66.5; stu.gender = 'M'; printf("%d %f %c\n", stu.age, stu.score, stu.gender); // 輸出各成員的內(nèi)容 return 0; }
方法2:結(jié)構(gòu)體指針變量名->成員名
#include <stdio.h> struct Student // 定義結(jié)構(gòu)體 { int age; float score; char gender; }; int main() { struct Student stu; // 定義結(jié)構(gòu)體變量 struct Student * pStu = &stu; //定義結(jié)構(gòu)體指針變量 // 對各成員賦值 pStu->age = 15; pStu->score = 66.5; pStu->gender = 'M'; printf("%d %f %c\n", stu.age, stu.score, stu.gender); // 輸出各成員的內(nèi)容 return 0; }
方法3:(*結(jié)構(gòu)體指針變量名).成員名
#include <stdio.h> struct Student // 定義結(jié)構(gòu)體 { int age; float score; char gender; }; int main() { struct Student stu; //定義結(jié)構(gòu)體變量 struct Student * pStu = &stu; //定義結(jié)構(gòu)體指針變量 // 對各成員賦值 (*pStu).age = 15; (*pStu).score = 66.5; (*pStu).gender = 'M'; printf("%d %f %c\n", stu.age, stu.score, stu.gender); // 輸出各成員的內(nèi)容 return 0; }
七、結(jié)構(gòu)體變量的傳參問題
當(dāng)我們在一個函數(shù)中定義了結(jié)構(gòu)體變量,而想要在另外一個函數(shù)中對該結(jié)構(gòu)體變量進行操作時,則需要向被調(diào)用的函數(shù)傳遞參數(shù),那傳遞結(jié)構(gòu)體變量作為參數(shù)還是應(yīng)該傳遞結(jié)構(gòu)體指針變量作為參數(shù)呢?這是一個必須要搞清楚的問題。我以下面的程序為例闡述不同情況下的傳參問題:
#include <stdio.h> #include <string.h> struct Student // 定義結(jié)構(gòu)體 { int age; float score; char name[100]; }; // 函數(shù)聲明 void input_student(struct Student* pStu); void output_student(struct Student* pStu); int main() { struct Student stu; // 定義結(jié)構(gòu)體變量 input_student(&stu); // 傳遞地址的原因詳見該程序下方 output_student(&stu); // 傳遞地址的原因詳見該程序下方 return 0; } // 賦值函數(shù) void input_student(struct Student * pStu) { (*pStu).age = 10; pStu->score = 66.5; strcpy(pStu->name, "南森"); /* 注意這里不能寫成 pStu->name = "南森"; 這是因為一維數(shù)組名是指針常量,不能對常量進行賦值操作 */ } // 輸出函數(shù) void output_student(struct Student* pStu) { printf("%d %f %s\n", (*pStu).age, pStu->score, pStu->name); }
為什么要傳遞地址?由于input_student
函數(shù)要修改主函數(shù)中結(jié)構(gòu)體變量stu
各個成員的值,所以只能傳輸?shù)刂?,不能傳輸變?code>stu,我已經(jīng)在《C語言之指針知識大總結(jié)》這篇文章中詳細敘述了其中的原因。而output_student
函數(shù)并不需要修改主函數(shù)中結(jié)構(gòu)體變量stu
各個成員的值,所以此時是可以傳輸變量stu
的,但由于變量stu
的類型是結(jié)構(gòu)體類型,其所占字節(jié)數(shù)較多,而所有的指針變量都只有四個字節(jié),所以為了減少內(nèi)存耗用并提高數(shù)據(jù)傳輸?shù)男?,往往傳輸對?yīng)的結(jié)構(gòu)體指針變量。
八、傳輸?shù)刂穾淼膯栴}
向一個函數(shù)傳入某個變量的地址不僅可以修改該變量的值,而且可以提高數(shù)據(jù)傳輸效率,但這樣做也會有一定的風(fēng)險。例如上一小結(jié)中的程序,其中的output_student
函數(shù)作為輸出函數(shù)并不需要在其內(nèi)部修改變量的值,而一旦傳入變量的地址,那就意味著output_student
函數(shù)也可以對該地址所對應(yīng)的變量進行修改,這樣就導(dǎo)致output_student
這個函數(shù)非常不安全,那怎么樣才能實現(xiàn)既可以快速傳輸數(shù)據(jù),又可以在不需要修改變量值的函數(shù)中禁止該函數(shù)修改數(shù)據(jù)呢?答案很簡單,只需要在形參的指針變量前加上關(guān)鍵字const
就可以達到這樣的目的。如下所示:
void output_student(const struct Student* pStu) { pStu->score = 88; // 錯誤,因為指針變量pStu前有const修飾,無法修改其對應(yīng)的普通變量的值 (*pStu).age = 10; // 錯誤,因為指針變量pStu前有const修飾,無法修改其對應(yīng)的普通變量的值 printf("%d %f %s\n", (*pStu).age, pStu->score, pStu->name); }
九、動態(tài)結(jié)構(gòu)體數(shù)組
#include <stdio.h> #include <malloc.h> // 定義結(jié)構(gòu)體 struct Student { char name[100]; int age; float score; }; // 函數(shù)聲明 int create_array(struct Student**); void input_array(struct Student*, int); void bubble_sort(struct Student*, int); void print(struct Student*, int); int main() { int length; struct Student* pStu; length = create_array(&pStu); // 由于要對指針變量pStu的內(nèi)容進行修改,所以只能傳輸其地址 input_array(pStu, length); bubble_sort(pStu, length); print(pStu, length); return 0; } // 該函數(shù)的作用是分配內(nèi)存并構(gòu)造數(shù)組 int create_array(struct Student** q) // 由于需要接收一級指針變量,所以這里需要使用二級指針 { int length; printf("請輸入學(xué)生人數(shù):"); scanf("%d", &length); printf("\n"); *q = (struct Student*)malloc(sizeof(struct Student) * length); // 動態(tài)分配內(nèi)存構(gòu)造結(jié)構(gòu)體數(shù)組 return length; } // 該函數(shù)的作用是對結(jié)構(gòu)體數(shù)組中的各元素進行賦值 void input_array(struct Student* p, int length) { int i; for (i = 0; i < length; i++) { printf("請輸入第%d個學(xué)生的信息:\n", i + 1); printf("姓名:"); scanf("%s", (p + i)->name); printf("年齡:"); scanf("%d", &(*(p + i)).age); printf("成績:"); scanf("%f", &p[i].score); printf("\n"); } } // 該函數(shù)的作用是按照結(jié)構(gòu)體數(shù)組中的score成員從低到高進行冒泡排序 void bubble_sort(struct Student* p, int length) { int i, j; struct Student t; for (i = 1; i < length; i++) for (j = 0; j < length - i; j++) if (p[j].score > p[j + 1].score) { // 注意:比較的是score的大小,但是需要交換的是結(jié)構(gòu)體數(shù)組中元素的位置,而不是只交換成員score t = p[j]; p[j] = p[j + 1]; p[j + 1] = t; } } // 該函數(shù)的作用是輸出結(jié)構(gòu)體數(shù)組中各元素的內(nèi)容 void print(struct Student* p, int length) { int i; printf("按照分數(shù)由低到高排序的結(jié)果為:\n"); for (i = 0; i < length; i++) { printf("姓名:%s\n", (p + i)->name); printf("年齡:%d\n", (*(p + i)).age); printf("成績:%f\n", p[i].score); printf("\n"); } }
十、關(guān)鍵字typedef
typedef關(guān)鍵字的作用是對數(shù)據(jù)類型起別名。例如:
#include <stdio.h> typedef int INTEGER; // 給int取了個別名叫INTEGER,故INTEGER就等同于int int main() { INTEGER i = 10; // 等同于 int i = 10; return 0; }
#include <stdio.h> typedef struct Student { char name[100]; int age; float score; }ST, * PST; // 給 struct Student 起了個別名ST,給 struct Student * 起了個別名叫PST int main() { ST stu; // 等同于 struct Student stu; PST pStu; // 等同于 ST * pStu; 也等同于 struct Student * pStu; return 0; }
十一、C++中的引用
使用C++中的引用在向其它函數(shù)傳參時可以幫助我們提高編碼效率,嚴蔚敏老師在《數(shù)據(jù)結(jié)構(gòu)》這本書中也多次使用了C++中的引用。
傳輸普通變量
C語言實現(xiàn)普通變量的傳輸:
#include <stdio.h> void modify(int* p) { (*p)++; } int main() { int i = 10; modify(&i); printf("%d\n", i); return 0; }
C++語言實現(xiàn)普通變量的傳輸:
#include <stdio.h> void modify(int& i) { i++; } int main() { int i = 10; modify(i); printf("%d\n", i); return 0; }
傳輸指針變量
C語言實現(xiàn)指針變量的傳輸:
#include <stdio.h> #include <malloc.h> void modify(int** q) { *q = (int *)malloc(sizeof(int)); } int main() { int * p; modify(&p); return 0; }
C++語言實現(xiàn)指針變量的傳輸:
#include <stdio.h> #include <malloc.h> void modify(int*& p) { p = (int *)malloc(sizeof(int)); } int main() { int * p; modify(p); return 0; }
到此這篇關(guān)于C語言全面梳理結(jié)構(gòu)體知識點的文章就介紹到這了,更多相關(guān)C語言結(jié)構(gòu)體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
本篇文章是對HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05C++類模板實戰(zhàn)之vector容器的實現(xiàn)
本文我們將做一個類模板實戰(zhàn)-手寫精簡版vector容器。讓我們自己封裝一個數(shù)組類,可以適應(yīng)基本數(shù)據(jù)類型和自定義數(shù)據(jù)類型,感興趣的可以了解一下2022-07-07C語言中動態(tài)內(nèi)存管理初學(xué)者容易犯的6個錯誤分享
本篇文章主要介紹了初學(xué)者使用C語言中動態(tài)內(nèi)存管理的4個函數(shù)時最容易犯的6個錯誤,以及如何避免這些錯誤,文中的示例代碼講解詳細,感興趣的可以了解一下2023-04-04c++中關(guān)于max_element()函數(shù)解讀
這篇文章主要介紹了c++中關(guān)于max_element()函數(shù)解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02