结构类型(枚举,结构,联合)
1. 枚舉
2. 結構
3. 聯合
枚舉
枚舉是?種?戶定義的數據類型,它?關鍵字 enum 以如下語 法來聲明:enum 枚舉類型名字 {名字0, …, 名字n} ;
枚舉類型名字通常并不真的使?,要?的是在?括號?的名字, 因為它們就是就是常量符號,它們的類型是int,值則依次從0 到n。如:enum colors { red, yellow, green } ;
就創建了三個常量,red的值是0,yellow是1,?green是2。
當需要?些可以排列起來的常量值時,定義枚舉的意義就是給 了這些常量值名字。
1. 常量符號化
#include <stdio.h>
const int red = 0;
const int yellow = 1;
const int green = 2;
// enum COLOR {RED, YELLOW, GREEN};
int main() {
int color = -1;
char *colorName = NULL;
printf("請輸入你喜歡的顏色的代碼:");
scanf("%d",&color);
switch(color){
case red:
colorName = "red";
break;
case yellow:
colorName = "yellow";
break;
case green:
colorName = "green";
break;
default:
colorName = "unknow";
break;
}
printf("你喜歡的顏色是%s\n",colorName);
return 0;
}
會報錯
?符號?不是具體的數字來表?程序中的數字
#include <stdio.h>
// const int red = 0;
// const int yellow = 1;
// const int green = 2;
enum COLOR {RED, YELLOW, GREEN};
int main() {
int color = -1;
char *colorName = NULL;
printf("請輸入你喜歡的顏色的代碼:");
scanf("%d",&color);
switch(color){
case RED:
colorName = "red";
break;
case YELLOW:
colorName = "yellow";
break;
case GREEN:
colorName = "green";
break;
default:
colorName = "unknow";
break;
}
printf("你喜歡的顏色是%s\n",colorName);
return 0;
}
輸入0,1,2來檢測輸出的結果
?枚舉?不是定義獨?的const int變量
2.案例
枚舉量可以作為值
枚舉類型可以跟上enum作為類型
但是實際上是以整數來做內部計算 和外部輸?輸出的
套路:?動計數的枚舉
#include <stdio.h>
enum COLOR {RED, YELLOW, GREEN, NumCOLORS};
int main() {
int color = -1;
char *ColorNames[NumCOLORS] = {
"red","yellow","green",
};
char *colorNames = NULL;
printf("請輸入你喜歡的顏色代碼:");
scanf("%d",&color);
if(color>=0 && color<NumCOLORS){
colorNames = ColorNames[color];
}else{
colorNames="unknown";
}
printf("你喜歡的顏色是%s\n",colorNames);
return 0;
}
4. 枚舉量
聲明枚舉量的時候可以指定值
enum COLOR { RED=1, YELLOW, GREEN = 5};
5.枚舉只是int
即使給枚舉類型的變量賦不存在的整數值也沒有任何 warning或error
6.枚舉
雖然枚舉類型可以當作類型使?,但是實際上 很(bu)少(hao)?
如果有意義上排?的名字,?枚舉?const int? 便
枚舉?宏(macro)好,因為枚舉有int類型
結構
1. 聲明結構類型
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
struct date{
int month;
int day;
int year;
};
struct date today;
today.month = 07;
today.day =31;
today.year = 2014;
printf("Today's date is%i-%i-%i.\n",today.year,today.month,today.day);
return 0;
}
2.在函數內/外?
和本地變量?樣,在函數內部聲明 的結構類型只能在函數內部使?
所以通常在函數外部聲明結構類型, 這樣就可以被多個函數所使?了
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct date{
int month;
int day;
int year;
};
int main() {
struct date today;
today.month = 07;
today.day =31;
today.year = 2014;
printf("Today's date is%i-%i-%i.\n",today.year,today.month,today.day);
return 0;
}
3.聲明結構的形式
4.結構變量
struct date today;
today.month=06;
today.day=19;
today.year=2005;
5. 結構的初始化
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct date{
int month;
int day;
int year;
};
int main() {
struct date today = {07,31,2014};
// struct date thismonth = {.month=7,.year=2014,.day=31};
printf("Today's date is %i-%i-%i.\n",today.year, today.month , today.day);
// printf("Today's date is%i-%i.\n",thismonth.year,thismonth.month,thismonth.day);
return 0;
}
6. 結構成員
結構和數組有點像
數組?[]運算符和下標訪問其成員
a[0] = 10;
結構?.運算符和名字訪問其成員
today.day
student.firstName
p1.x
p1.y
7. 結構運算
要訪問整個結構,直接?結構變量的名字
對于整個結構,可以做賦值、取地址,也可以 傳遞給函數參數
p1 = (struct point){5, 10}; // 相當于p1.x = 5;
p1.y = 10;
p1 = p2; // 相當于p1.x = p2.x; p1.y = p2.y;
8.復合字?量
today = (struct date) {9,25,2004};
today = (struct date) {.month=9, .day=25, year=2004};
9.結構指針
和數組不同,結構變量的名字并不是 結構變量的地址,必須使?&運算符
struct date *pDate = &today;
10.結構與函數
結構作為函數參數
int numberOfDays(struct date d);
11.輸?結構
沒有直接的?式可以?次scanf? 個結構
如果我們打算寫?個函數來讀? 結構 : —>
但是讀?的結構如何送回來呢?
記住C在函數調?時是傳值的
所以函數中的p與main中的y是不 同的
在函數讀?了p的數值之后,沒 有任何東?回到main,所以y還 是 {0, 0}
12.解決的?案
之前的?案,把?個結構傳?了函數,然后在函數中操 作,但是沒有返回回去
問題在于傳?函數的是外?那個結構的克隆體,?不是指 針
傳?結構和傳?數組是不同的
在這個輸?函數中,完全可以創建?個臨時的結構變量, 然后把這個結構返回給調?者
13.結構指針作為參數
14.指向結構的指針
15.結構指針參數
16.結構數組
struct date dates[100];
struct date dates[] = {
{4,5,2005},{2,4,2005}};
17.結構中的結構
struct dateAndTime {
struct date sdate;
struct time stime;
};
18.嵌套的結構
19.結構中的結構的數組
聯合
1.?定義數據類型
C語?提供了?個叫做 typedef 的功能來聲明?個已有的數據類型的
新名字。?如:typedef int Length; 使得 Length 成為 int 類型的別名。
這樣, Length 這個名字就可以代替int出現在變量定義和參數聲明的
地?了: Length a, b, len ; Length numbers[10] ;
2.Typedef
聲明新的類型的名字
新的名字是某種類型的別名
改善了程序的可讀性
3. typedef
typedef struct {
int month;
int day;
int year;
} Date;
4. 聯合
存儲
所有的成員共享?個空間
同?時間只有?個成員是有效的
union的??是其最?的成員
初始化
對第?個成員做初始化
結構指針參數
總結
以上是生活随笔為你收集整理的结构类型(枚举,结构,联合)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 姓邱的牛虎女孩子起名大全(寻找与姓邱相配
- 下一篇: Vue路由讲解