C語言中結(jié)構(gòu)體實例解析
一.結(jié)構(gòu)體定義
C語言結(jié)構(gòu)體由一系列相同或者不同類型的數(shù)據(jù)構(gòu)成的集合,結(jié)構(gòu)體類型就是以struct關(guān)鍵字定義的數(shù)據(jù)類型。
結(jié)構(gòu)體的格式如下:
struct 結(jié)構(gòu)名稱 {
結(jié)構(gòu)體所包含的數(shù)據(jù)成員,包括變量數(shù)組等
} 結(jié)構(gòu)變量 ;//結(jié)構(gòu)變量可以指定一個或多個
舉例:
struct Student
{
char name[10];
char sex[2];
int age;
}Stu
二.實例演示
先看下結(jié)構(gòu)體變量如何初始化

#include <stdio.h>
#include <string.h>
struct Student
{
char name[10];
char sex[5];
int age;
}Stu = {"Mike","man",22};
int main(int argc, char *argv[])
{
printf("name:%s\nsex:%s\nage:%d\n",Stu.name,Stu.sex,Stu.age);
}
初始化結(jié)構(gòu)體變量很簡單,直接在結(jié)構(gòu)體變量后面賦值。
結(jié)果:

結(jié)構(gòu)體作為函數(shù)參數(shù)
#include <stdio.h>
#include <string.h>
//定義Student結(jié)構(gòu)體
struct Student
{
char name[10];
char sex[5];
int age;
}Stu;
void print(struct Student stu)
{
printf("Student name:%s\n",stu.name);
printf("Student sex:%s\n",stu.sex);
printf("Student age:%d\n",stu.age);
}
int main(int argc, char *argv[])
{
struct Student stu1;
strcpy(stu1.name,"will");
strcpy(stu1.sex,"man");
stu1.age = 20;
print(stu1);
//Stu
Stu.age=11;
print(Stu);
}

從這個示例可以看出:將結(jié)構(gòu)體作為參數(shù)傳入函數(shù),定義結(jié)構(gòu)體時,我們可以在;前面定義結(jié)構(gòu)體變量, 這樣就不需要再定義結(jié)構(gòu)變量,如:struct Student stu1;假設(shè)stu1在定義結(jié)構(gòu)體時就定義變量,那么就可以直接賦值。
結(jié)果:

可以看出第二個學(xué)生打印,因為在定義結(jié)構(gòu)體時就已經(jīng)定義結(jié)構(gòu)變量,所以可以直接賦值。
結(jié)構(gòu)體指針
實例演示,傳入結(jié)構(gòu)體指針

#include <stdio.h>
#include <string.h>
struct Student
{
char name[10];
char sex[5];
int age;
}Stu;
void print(struct Student *stu)
{
printf("Student name:%s\n",stu->name);
printf("Student sex:%s\n",stu->sex);
printf("Student age:%d\n",stu->age);
}
int main(int argc, char *argv[])
{
struct Student stu1;
strcpy(stu1.name,"will");
strcpy(stu1.sex,"man");
stu1.age = 20;
print(&stu1);
Stu.age=11;
print(&Stu);
}
這里的實例和上面例子的區(qū)別主要是:
1.將定義的變量改為指針struct Student *stu。
2.指針賦值時使用->。
3.使用打印函數(shù)時,改為取地址。
結(jié)果一致
三.typedef struct 和 struct的區(qū)別
1、聲明不同
1)、struct:struct可以直接使用結(jié)構(gòu)體名字聲明結(jié)構(gòu)體。
2)、typedef struct:typedef struct為修飾結(jié)構(gòu)體,結(jié)構(gòu)體有了別名,通過結(jié)構(gòu)體別名聲明結(jié)構(gòu)體。
2、訪問成員變量不同
1)、struct:struct定義的結(jié)構(gòu)體變量,可直接訪問結(jié)構(gòu)體成員。
2)、typedef struct:typedef struct定義的結(jié)構(gòu)體變量,不可直接訪問結(jié)構(gòu)體成員,必須顯式的通過結(jié)構(gòu)體變量來訪問成員。
3、重新定義不同
1)、struct:想重新定義struct結(jié)構(gòu)體的話,必須重寫整個結(jié)構(gòu)體。
2)、typedef struct:想重新定義typedef struct結(jié)構(gòu)體的話,可以通過別名來繼承結(jié)構(gòu)體進(jìn)行重新定義。
舉例:

可以看到:
使用typedef struct定義的結(jié)構(gòu)體,我們通常是使用別名進(jìn)行操作,而且在使用時也簡化了使用方法,例如:Stu s1,相當(dāng)于聲明對象一樣,如果使用struct,那么就要寫成struct Student stu1;。
如果直接使用結(jié)構(gòu)體名稱那么會報錯:
錯誤示例:
報錯結(jié)果:
改回別名操作,結(jié)果:

總結(jié)
到此這篇關(guān)于C語言中結(jié)構(gòu)體實例解析的文章就介紹到這了,更多相關(guān)C語言結(jié)構(gòu)體內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++ Qt 數(shù)據(jù)庫與Chart歷史數(shù)據(jù)展示
這篇文章主要介紹了Qt利用Qchart組件展示數(shù)據(jù)庫中的歷史數(shù)據(jù)。文中的示例代碼講解清晰,具有一定的學(xué)習(xí)和工作價值,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12
C語言動態(tài)鏈表實現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言動態(tài)鏈表實現(xiàn)學(xué)生學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
C語言實現(xiàn)電話簿管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)電話簿管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11



