C Ch12 重點整理-Structure

透過 structures, union, 及 enumberated type ,C 語言提供了豐富的 data types。

Structure

Struct structure的名{

    Type1 變數名1;

    Type2 變數名2;

    …

}變數名;

如:

struct car{
    int color;
    int passengers;
}blue_car;<span style="font-family: Arial; background-color: rgb(255, 255, 255);"> </span>

亦可將structure的名省略,用anonymous structure的方式來產生變數,如:

struct {
    int color;
    int passengers;
}blue_car;

也可以用這種方式,

struct car{
    int color;
    int passengers;
};

然後用struct car去宣告變數,如:

struct car blue_car;<span style="font-family: Arial; background-color: rgb(255, 255, 255);"> </span>

也可以用以下的方式去initialize structure內的元素,如:

#include <stdio.h>
struct car{
    char type_name[10];
    int color;
    int passenger;
    intcost;
}blue_car={
    "little",
           0,
           4,
    10000
};
 
int main()
{
     printf("%d,%d\n",blue_car.color,blue_car.cost);
     return 0;
}
 

Union

Union很類似 structure但是他定義一個可以給很多不同field names使用的單一位置。你可以將struct想像會一個box包裏著裡面名稱不同的元件;而union則是一個box,隨時被不同名稱的box佔有使用,而非共享:

如:

union value{
    long int i_value;
    float f_value;
};
value只能是 i_value或者为 f_value,说明例子如下:
#include <stdio.h>
union value{
   long int i_value;
   float f_value;
} data;
int i;
float f;
 
int main()
{
    data.f_value=5.0;
    data.i_value=3;   //此時union會成integer的i_value
    i=data.i_value;   //整數=整數 ==>合法
    f=data.f_value;   //浮點數=整數 ==>會產生不可測的結果
    data.f_value=5.1;//此時union會成浮點數的f_value
    i=data.f_value;   //整數=浮點數 ==>會產生不可測的結果
    printf("%d\n%f\n",i,f);
    return 0;
}
 

typedef

C 允許程式設計者利用typedef 去定義自己的變數type,格式如下:

typedef 預定的type名;

如:

#include<stdio.h>
main()
{
    typedef float height;
    height a=1.8;
    printf("%f\n",a);
    return 0;
}

比較常用的是struct又如:

#include<stdio.h>
 
struct car_struct{
    char type_name[10];
    int color;
    int passenger;
    int cost;
};
typedef struct car_struct car;
car blue_car={"a little",1,2,3000};
intmain()
{
    printf("%d,%d\n",blue_car.color,blue_car.cost);
    return 0;
}
又或者直接用以下的方式:

 #include<stdio.h>
 
typedef struct car_struct{
    char type_name[10];
    int color;
    int passenger;
    int cost;
} car;
 
car blue_car={"a little",1,2,3000};
int main()
{
    printf("%d,%d\n",blue_car.color,blue_car.cost);
    return 0;
}


Enum 

如果想要將一堆名詞定義成連續數字,便會使用 enum type ,使用格式如下:

enum enum名{第一個,第二個…}變數名

如:

#include<stdio.h>
enum month {jan,feb,march,apr, may,jun, jul,aug,sep,oct,nov,dec};
intmain()
{
    enum monththis_month=jan;
    printf("%d\n",this_month); //然後會print出 0,因為從0開始
    return 0;
}<span style="font-family: Arial; background-color: rgb(255, 255, 255);"> </span>

Casting

這種type間的變換,在C 也有,格式為:

(type)expression

如:

#include<stdio.h>
intmain()
{
    int total_ppl_number=5;
    int total_score=190;
    float avg=((float)total_score)/((float)total_ppl_number);
    printf("avg score in this class is%f\n",avg); //可以算出平均成績
    return 0;
}

 

Packed Structure

C 語言的Structure可以控制其元素所佔bit數,在容量有限的情況下才使用,不然不建議使用。如以下的structure 在16bit的機器,會佔6 byte:

struct item{
    unsigned int list;             //Boolean
    unsigned int seen;           //Boolean
    unsigned int number;        //item號碼,需要1000件
};
但 boolean 只需要0、1,用1bit,就夠了,而我們的item號碼,只要從0~999 (10bit)就可以了,整個struct其實只要16bits,machine讀一次即可,作法如下:

struct item{
    unsigned int list:1;             //Boolean
    unsigned int seen:1;           //Boolean
    unsigned int number:10;        //item號碼,需要1000件
};

練習一:寫一個structure 可以hold mailing list,並寫一個function可以列印出資料

#include <stdio.h>
typedef struct mail{
    char sender[30];
    char receiver[30];
    int zip_code;
} mail;

void print_mail(mail a)
{
    printf("this mail's sender address is: %s\nreceiver address is: %s\nzip code is: %d\n",a.sender,a.receiver,a.zip_code);

}
int main()
{
    mail love_letter={"shanwu","dorothy",21412};
    print_mail(love_letter);
    return 0;
}

練習二:寫一個能hold住date及時間的structure並寫一個能計算時間差的程式
#include <stdio.h>
typedef struct time{
    int year;
    int month;
    int day;
    int hour;
    int min;
    int sec;
} time;
void find_diff(time a, time b)
{
    int diff=0;
    int y=(a.year-b.year)*365*24*60*60;
    int mo=(a.month-b.month)*30*24*60*60;
    int d=(a.day-b.day)*24*60*60;
    int h=(a.hour-b.hour)*60*60;
    int mi=(a.min-b.min)*60;
    int s=(a.sec-b.sec);
    diff=y+mo+d+h+mi+s;
    if(diff<0)
    {
        diff=(-diff);    
    }
    printf("difference is %d seconds\n",diff);
}
int main()
{
    time a={2010,11,12,11,57,12};
    time b={2010,11,12,11,48,02};
    find_diff(a,b);
    return 0;
}

指向结构变量的指针
结构指针变量说明的一般形式为:
     struct 结构名 *结构指针变量名;
范例如下:
#include <stdio.h>
typedef struct{
   int face_score;
   float bust;
   char cup;
   int waist;
   int hip;
}beauty; 

main()
{     
    beauty A={95,90,'A',70,75};
    beauty B={94,95,'F',56,60};
    beauty *b_ptr; 
    b_ptr=&A; 
    // (*b_ptr).cup = b_ptr->cup  
    printf("Miss A is %c cup\n",b_ptr->cup);
    b_ptr=&B;
    // (*b_ptr).cup = b_ptr->cup  
    printf("Miss B is %c cup\n",(*b_ptr).cup);
}
  图片

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值