pythonc语言结构_C语言结构体笔记
typedef給結(jié)構(gòu)起別名
可以是匿名結(jié)構(gòu)或者普通的結(jié)構(gòu),方便后面使用。
#include
typedef struct{ //匿名結(jié)構(gòu)
float tank_capacity;
int tank_psi;
const char *suit_material;
} equipment; //結(jié)構(gòu)的別名
typedef struct scuba{ //結(jié)構(gòu)名
const char *name;
equipment kit;
} diver;
void badge(diver d){
//使用“點表示法”訪問結(jié)構(gòu)的值,不能使用下標(biāo)
printf("Name:%s Tank:%2.2f(%i) Suite:%s\n",d.name,d.kit.tank_capacity,d.kit.tank_psi,d.kit.suit_material);
};
int main(){
diver randy = {"Randy",{5.5,3500,"Neoprene"}};
badge(randy);
return 0;
}
給結(jié)構(gòu)的元素賦值
在C語言中,當(dāng)為結(jié)構(gòu)賦值時,計算機會復(fù)制結(jié)構(gòu)的值。所以需要結(jié)構(gòu)指針
typedef sturct{
const char *name;
int age;
} turtle;
turtle t={"lisa",12}
(*t).age和*t.age含義不同
t.age等于(t.age),它代表這個存儲器單元中的內(nèi)容。
為了方便閱讀通常將(*t).age寫作另一種形式
(*t).age==t->age
t->age表示"由t指向結(jié)構(gòu)中的age字段"。
指定初始化器的方式
#include
typedef struct{
const char* color;
int gears;
int height;
} bike;
int main(){
bike b= {.height=3,.color="red"};
printf("%d\n",b.height);
printf("%s\n",b.color);
printf("%d\n",b.gears);
return 0;
}
我們還可以通過.屬性名的方式指定初始化器,對指定對象賦值,其他的不變
總結(jié)
以上是生活随笔為你收集整理的pythonc语言结构_C语言结构体笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python第二大奇数_python-2
- 下一篇: 多线程 python layer_在Ca