第十講:C語言基礎之結構體和共用體(上)

學思而勤行 發佈 2020-01-10T11:00:46+00:00

如:一個學生有學號/姓名/性別/年齡/地址等屬性int num; char name[20]; char sex; int age; int char addr[30];定義一個結構的一般形式為: struct 結構名 {成員表列};成員表列由若干個成員組成,每個成員都是該結構的

本文約4500字,主要講了結構體和共用體。

帶你進入C語言的世界,入門C語言,後邊將持續更新。可以收藏學習。想了解無參宏定義、帶參宏定義以及案例等,請點擊該處跳轉閱讀。

本文全乾貨,新手必備,感謝你的閱讀,祝你學有所成,生活愉快!


一、概述

問題定義:有時需要將不同類型的數據組合成一個有機的整體,以便於引用。

如:一個學生有學號/姓名/性別/年齡/地址等屬性 int num; char name[20]; char sex; int age; int char addr[30];

定義一個結構的一般形式為:

struct 結構名

{成員表列};

成員表列由若干個成員組成,每個成員都是該結構的一個組成部分。對每個成員也必須作類型說明,其形式為:

類型說明符 成員名;

例如:

struct student

{ int num;

char name[20];

char sex;

int age;

float score;

char addr[30];};

二、定義結構體類型變量的方法

可以採取以下3種方法定義結構體類型變量:

(1)先聲明結構體類型再定義變量名


定義了student1和student2為struct student類型的變量,即它們具有struct student類型的結構。

例如:

struct student

{

int num;

char name[20];

char sex;

int age;

float score;

char addr[30];

} student1, student2

在定義了結構體變量後,系統會為之分配內存單元。

例如: student1和student2在內存中各占多少個字節。

(4 + 20 + 1 + 4 + 4 + 30 =67)。

(2)在聲明類型的同時定義變量

這種形式的定義的一般形式為:

struct 結構體名

{成員表列

}變量名表列;

例如:

struct student

{ int num;

char name[20];

char sex;

int age;

float score;

char addr[30];

}student1,student2;

(3) 直接定義結構體類型變量。其一般形式為:

struct

{成員表列

}變量名表列; //即不出現結構體名。

例題1:

首先定義一個結構date,由month(月)、day(日)、year(年) 三個成員組成。

在定義並說明變量 boy1 和 boy2 時,其中的成員birthday被說明為data結構類型。成員名可與程序中其它變量同名,互不干擾。

結構嵌套:

struct date

{

int month;

int day;

int year;

};

struct

{ int num;

char name[20];

char sex;

struct date birthday;

float score;

}boy1, boy2;

三、結構體變量的引用

在定義了結構體變量以後,當然可以引用這個變量。但應遵守以下規則:

(1) 不能將一個結構體變量作為一個整體進行輸入和輸出

例如:列印student1的各個變量的值。可以這樣嗎?

錯誤示例:printf(″%d,%s,%c,%d,%f,%\n″,student1);

正確引用結構體變量中成員的方式為:結構體變量名.成員名

student1.num表示student1變量中的num成員,即student1的num(學號)項。可以對變量的成員賦值,例如:student1.num=100;

「.」是成員(分量)運算符,它在所有的運算符中優先級最高,因此可以把student1.num作為一個整體來看待。上面賦值語句的作用是將整數100賦給student1變量中的成員num。

例題2:

#include <stdio.h>

main()

{

struct student

{

int num;

char *name;

char sex;

float score;

} boy1, boy2;


boy1.num = 007;

boy1.name = "Jane";


printf("Please input sex and score\n");

scanf("%c %f", &boy1.sex, &boy1.score);


boy2 = boy1;


printf("Number = %d\nName = %s\n", boy2.num, boy2.name);

printf("Sex = %c\nScore = %f\n", boy2.sex, boy2.score);

}

(2) 如果成員本身又屬一個結構體類型,則要用若干個成員運算符,一級一級地找到最低的一級的成員。只能對最低級的成員進行賦值或存取以及運算。

對上面定義的結構體變量student1, 可以這樣訪問各成員:

student1.num

student1.birthday.month

(3) 對結構體變量的成員可以像普通變量一樣進行各種運算(根據其類型決定可以進行的運算)。

例如:

student2.score = student1.score;

sum = student1.score + student2.score; student1.age++;

++student2.age;

(4) 可以引用結構體變量成員的地址,也可以引用結構體變量的地址。但不能用以下語句整體讀入結構體變量:

scanf(″%d,%s,%c,%d,%f,%s″,&student1);

結構體變量的地址主要用作函數參數,傳遞結構體變量的地址。

例題3:

#include <stdio.h>

main()

{

struct student

{

int num;

char *name;

char sex;

float score;

} boy1;


boy1.num = 007;

boy1.name = "Jane";


printf("The address of struct is %o :\n", &boy1 );

printf("The address of num is %o :\n", &boy1.num );

}

四、結構體變量的初始化

案例:

struct student /*定義結構*/

{ int num;

char *name;

char sex;

float score;

}boy1, boy2 = { 102, "Jane", 'M', 98.5 };

例題4:

#include <stdio.h>

main()

{

struct student /*定義結構*/

{

int num;

char *name;

char sex;

float score;

}boy1, boy2 = { 102, "Jane", 'M', 98.5 };


boy1 = boy2;

printf("Number = %d\nName = %s\nScore = %d\n", boy1.num, boy1.name, boy1.score);

printf("\n\n");

printf("Number = %d\nName = %s\nScore = %d\n", boy2.num, boy2.name, boy2.score);


}

五、結構體數組

一個結構體變量中可以存放一組數據(如一個學生的學號、姓名、成績等數據)。如果有10個學生的數據需要參加運算,顯然應該用數組,這就是結構體數組。

結構體數組與以前介紹過的數值型數組不同之處在於每個數組元素都是一個結構體類型的數據,它們都分別包括各個成員(分量)項。

例題5:

#include"stdio.h"

#define NUM 3

struct person

{

char name[20];

char phone[10];

};

main()

{

struct person man[NUM];

int i;

for( i=0; i < NUM; i++)

{

printf("input name:\n");

gets(man[i].name);

printf("input phone:\n");

gets(man[i].phone);

}

printf("name\t\t\tphone\n\n");

for( i=0; i < NUM; i++)

{

printf("%s\t\t\t%s\n",man[i].name,man[i].phone);

}

}

1.定義結構體數組

和定義結構體變量的方法相仿,只需說明其為數組即可。

例如:

struct student

{

int num;

char name[20];

char sex;

int age;

float score;

char addr[30];

};

struct student student[10];

或:

struct student

{

int num;

char name[20];

char sex;

int age;

float score;

char addr[30];

}student[10];

2.結構體數組的初始化

與其他類型的數組一樣,對結構體數組可以初始化。

例如:

struct student

{int num;

char name[20];

char sex;

int age;

float score;

char addr[30];

}stu[2]={{101,″LiLin″,′M′,18,87.5,″Beijing″}, {102,″Zhang″,′F′,19,99,″Shanghai″}};

當然,數組的初始化也可以用以下形式:

struct student

{int num;

…};

struct student str[ ]{{…},{…},{…}};

即先聲明結構體類型,然後定義數組為該結構體類型,在定義數組時初始化。

例題6:對候選人得票的統計程序。設有3個候選人,每次輸入一個得票的候選人的名字,要求最後輸出各人得票結果。

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define NUM 4

struct person

{

char name[20];

int count;

}candidate[NUM] = {

{"A", 0},

{"B", 0},

{"C", 0},

{"D", 0}

};

char *winner();

main()

{

int i, j;

char candidate_name[20];

printf("歡迎進入良好公民評選投票系統:() \n\n");

printf("候選人有: 小甲魚, 蒼井空, 松島楓, 莫丁丁(路人甲)\n\n");

for( i=1; i <= 10; i++ )

{

printf("第 %2d 位投票, 請寫下支持的候選人名字: ", i);

scanf("%s", candidate_name);

for( j=0; j < NUM; j++ )

{

if( 0 == strcmp(candidate_name, candidate[j].name) )

{

candidate[j].count++;

}

}

}

printf("\n");

for( i=0; i < 4; i++ )

{

printf("%s 同學得票數為: %d\n", candidate[i].name, candidate[i].count );

}

printf("\n");

printf("本次投票活動的勝利者的: %s", winner() );

printf("\n");

system("pause");

}

char *winner()

{

int i =0 , winner = i;

for( i=1; i < NUM; i++ )

{

if( candidate[winner].count < candidate[i].count )

{

winner = i;

}

}

return candidate[winner].name;

}



感謝您的閱讀,希望有所收穫!會持續更新!

關鍵字: