数据结构,联合和列举(Structures, Unions, and Enumerations)

返回

枚举类型(enum)例子

#include <stdio.h> int main(void) { enum types {BLUE, BLACK, RED, YELLOW, WHITE} color; int i, int2; color = BLUE; printf("color=>%d\n", color); printf("BLACK=>%d\n", BLACK); int2 = YELLOW; printf("int2 =>%d\n", int2); for (i=BLUE; i<=WHITE; i++) printf("i=>%d\n", i); return 0; } /* 执行结果 color=>0 BLACK=>1 int2 =>3 i=>0 i=>1 i=>2 i=>3 i=>4 */

数据结构程序例子

#include <stdio.h> #include <string.h> struct jiage { int dinjia; int jianjia; int yudai; }; typedef struct goods { char name[30]; struct jiage kakaku; int kucun; } goods; int main(void) { goods syo; goods *sp = &syo; strcpy(syo.name,"PC486-3"); syo.kakaku.dinjia = 19000; syo.kakaku.jianjia = 18000; syo.kakaku.yudai = 17200; syo.kucun = 136; printf("GOODS :%s\n", sp->name); printf("Dingjia:%d\n", sp->kakaku.dinjia); printf("Jianjia:%d\n", sp->kakaku.jianjia); printf("Yu--dai:%d\n", sp->kakaku.yudai); printf("Ku--cun:%d\n", sp->kucun); return 0; } /* 运行结果 GOODS :PC486-3 Dingjia:19000 Jianjia:18000 Yu--dai:17200 Ku--cun:136 */ 以下两个宣言的区别是什么? struct x1 {...}; typedef struct {...} x2; 前者是一个结构宣言;后者是一个"typedef"。 参照时,前者用"struct x1";后者用"x2"。
#include <stdio.h> typedef struct Styp { char ss[80]; int nn; } Styp; struct Styp s1 = {"Test for typedef",100}; int main(void) { printf("s1 = %s\n", s1.ss); printf("n1 = %i\n", s1.nn); return 0; } /* s1 = Test for typedef n1 = 100 */
返回