您當前位置>首頁 » 新聞資訊 » 技(jì)術(shù)分(fēn)享 >
C語言學習(xí)入門(mén)(九)typedef關鍵字
發表時(shí)間(jiān):2021-1-10
發布人(rén):葵宇科(kē)技(jì)
浏覽次數(shù):44
關鍵字:typedef
用(yòng)法:為(wèi)各種數(shù)據類型定義一(yī&≈)個(gè)新名字(别名)
typedef與基本數(shù)據類型
typedef int Inte>✔ger;Integer a= 8;
也(yě)可(kě)以在别名的(de)基礎上(shàng)再∑σ起一(yī)個(gè)别名
typedef Integer MyInteger;&☆ βMyInteger a = 8;
原來(lái)的(de)數(shù)據類型也(yě)可(kě)以正常使用(yòng)
typedef與指針
typedef char *String;String s♣<tr = “jackie”;
typedef與結構體(tǐ)
typedefstructPerson Per;// 這(zhè)樣在定義結φ↓構體(tǐ)變量時(shí) 就(jiù)不(bù)用(yòng)帶上(shàng)struπ&×ct 關鍵字了(le)
Per p; p.name = “xyz”;
定義并取别名:
typedefstruct Student//★•∏π 結構體(tǐ)名 Student 可(kě)以省略λ>→
{
int age;
} Stu;
void processStudent()
{
Stu studeλ$<nt = {18};
student.age ≈<♦;=19;
}
typedef與指向結構體(tǐ)的(de)指針
typedef struct
{
int age;
} Stu;
Stu stu = {20};
typedef Stu *S;//指向結構體(tǐ)的(de)指>§針 取别名 S
S s = &stu;
typedef struct LNode
{
int data;
struct LNode *next;
} LinkList, *SList;
int main(int argc, const char * arg₽®v[])
{
LinkList l = {1, NULL};
LinkList ll = {2§, NULL};
l.next = ≪
printf("%d, &q§uot;, l.next->data);
SList sl = &ll→↕γ;
if (sl->nex≈λt != NULL)
printf("%d, "•';, sl->data);
return 0;
}
typedef與枚舉類型
typedef enum
{
…
} Season;
//用(yòng)法與結構體(tǐ)類似
typedef與指向函數(shù)的(de)指針
int sum(int a, int b)
{
return a + b;
}
void main()
{
typedef int (*P)(int a, int b);
P p = sum;
int result = (*p)(3, 5);
return 0;
}
typedef與#define
typedef char *String;
String s = “abc”
#define String char *;
String s = “abc”; //這(zhè)樣使用(yòng)效∞Ω果一(yī)樣
當 這(zhè)樣使用(yòng):
String s1,s2; //用(yòng)第一(€™±yī)種替換: char *s1, char *s2;
String s3,s4; //用(yòng)第二種替換: ≤€↕βchar * s3, s4; <==> cha ∞r *s3, char s4;